
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
    <title>dbm &#8212; Texas A&amp;M Oil spill / Outfall Calculator 2.1.0 documentation</title>
    <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
   
  <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for dbm</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">DBM Module</span>
<span class="sd">==========</span>

<span class="sd">Define objects that interface with the DBM functions in ``dbm_f``.</span>

<span class="sd">This module defines high-level Python class objects that wrap the individual </span>
<span class="sd">functions that comprise the Discrete Bubble Model (DBM) in ``dbm_f``.  </span>

<span class="sd">These are particularly useful as an interface to the chemical property data</span>
<span class="sd">contained in ``./data/ChemData.csv`` and for pre- and post-processing of data</span>
<span class="sd">needed by the ``TAMOC`` simulation modules. These classes provide efficient </span>
<span class="sd">data management of the chemical properties needed by the ``dbm_f`` functions </span>
<span class="sd">and ensure proper behavior for the equations of state.</span>

<span class="sd">Notes</span>
<span class="sd">-----</span>
<span class="sd">The functions defining most equations of state and fluid particle physics are</span>
<span class="sd">contained in the ``dbm_f`` library.  ``dbm_f`` contains object code compiled</span>
<span class="sd">from the Fortran sources::</span>

<span class="sd">    ./src/dbm_eos.f95</span>
<span class="sd">    ./src/dbm_phys.f95</span>
<span class="sd">    ./src/math_funcs.f95</span>

<span class="sd">There are two additional functions defined in this module to complete the </span>
<span class="sd">equations of state calculations.  These are::</span>

<span class="sd">    dbm.equilibrium</span>
<span class="sd">    dbm.gas_liq_eq</span>

<span class="sd">which compute the partitioning between gas and liquid of each component in </span>
<span class="sd">a mixture.  For this calculation, iteration is required until the fugacities </span>
<span class="sd">of each component in the mixture are equal in both gas and liquid.  Because </span>
<span class="sd">for ``TAMOC`` this would generally only be done once at the start of a </span>
<span class="sd">simulation to establish initial conditions and because `scipy.optimize` </span>
<span class="sd">provides a nice Python interface to a fast zero-solver, these two elements of </span>
<span class="sd">the discrete bubble model have not been ported to Fortran and reside in the </span>
<span class="sd">`dbm` module instead of the ``dbm_f`` library.  </span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="c1"># S. Socolofsky, July 2013, Texas A&amp;M University &lt;socolofs@tamu.edu&gt;.</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="p">(</span><span class="n">absolute_import</span><span class="p">,</span> <span class="n">division</span><span class="p">,</span> <span class="n">print_function</span><span class="p">)</span>

<span class="kn">from</span> <span class="nn">tamoc</span> <span class="kn">import</span> <span class="n">chemical_properties</span>
<span class="kn">from</span> <span class="nn">tamoc</span> <span class="kn">import</span> <span class="n">dbm_f</span>
<span class="kn">from</span> <span class="nn">tamoc</span> <span class="kn">import</span> <span class="n">seawater</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">scipy.optimize</span> <span class="kn">import</span> <span class="n">fsolve</span>

<div class="viewcode-block" id="FluidMixture"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.html#dbm.FluidMixture">[docs]</a><span class="k">class</span> <span class="nc">FluidMixture</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Class object for a fluid mixture</span>
<span class="sd">    </span>
<span class="sd">    This object defines the behavior of a fluid mixture defined as a standard</span>
<span class="sd">    thermodynamic system.  The mixture may contain just liquid phase, a </span>
<span class="sd">    gas and liquid phase together, or a pure gas phase.  The Peng-Robinson</span>
<span class="sd">    equation of state returns the properties of each phase in the mixture.</span>
<span class="sd">    If the mixture is pure liquid or pure gas, the properties of each phase</span>
<span class="sd">    will be the same; otherwise, the gas properties will be in the first </span>
<span class="sd">    row of all two-dimensional return variables and the liquid properties in </span>
<span class="sd">    the second row.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    composition : string list, length nc</span>
<span class="sd">        Contains the names of the chemical components in the mixture</span>
<span class="sd">        using the same key names as in ./data/ChemData.csv</span>
<span class="sd">    delta : ndarray, size (nc, nc)</span>
<span class="sd">        Binary interaction coefficients for the Peng-Robinson equation of </span>
<span class="sd">        state.  If not passed at instantiation, Python will assume a </span>
<span class="sd">        full matrix of zeros.</span>
<span class="sd">    user_data : dict</span>
<span class="sd">        A dictionary of chemical property data.  If not specified, the data</span>
<span class="sd">        loaded from `/tamoc/data/ChemData.csv` by ``chemical_properties`` will</span>
<span class="sd">        be used.  To load a different properties database, use the </span>
<span class="sd">        ``chemical_properties.load_data`` function to load in a custom </span>
<span class="sd">        database, and pass that data to this object as `user_data`.</span>
<span class="sd">    delta_groups : ndarray (nc, 15)</span>
<span class="sd">        Provides the group contribution numbers (normalized) for each </span>
<span class="sd">        component in the mixture for the 15 groups used by the Privat and</span>
<span class="sd">        Jaubert (2012) group contribution method for binary interaction </span>
<span class="sd">        coefficients.  Default is None, in which case the values in `delta`</span>
<span class="sd">        will be used.</span>
<span class="sd">    air : bool</span>
<span class="sd">        Flag indicating whether or not fluid is air.  The methods for </span>
<span class="sd">        viscosity and interfacial tension below use correlations developed</span>
<span class="sd">        for hydocarbons.  If `air` is False (default value), these built</span>
<span class="sd">        in methods are used.  If `air` is True, then these methods are </span>
<span class="sd">        replaced with correlations between air and seawater.</span>
<span class="sd">    sigma_correction : ndarray, default = np.array([[1], [1]])</span>
<span class="sd">        Correction factor to adjust the interfacial tension value supplied by</span>
<span class="sd">        the default model to a value measured for the mixture of interest.</span>
<span class="sd">        The correction factor should be computed as sigma_measured / </span>
<span class="sd">        sigma_model at a single P and T value.  sigma_correction should be a</span>
<span class="sd">        two-element array, the first element is for the gas phase and the </span>
<span class="sd">        second element is for the liquid phase.  </span>
<span class="sd">    </span>
<span class="sd">    Attributes</span>
<span class="sd">    ----------</span>
<span class="sd">    nc : integer</span>
<span class="sd">        Number of chemical components in the mixture</span>
<span class="sd">    issoluble : logical, True</span>
<span class="sd">        Indicates the object contents are soluble</span>
<span class="sd">    M : ndarray, size (nc)</span>
<span class="sd">        Molecular weights (kg/mol)</span>
<span class="sd">    Pc : ndarray, size (nc)</span>
<span class="sd">        Critical pressures (Pa)</span>
<span class="sd">    Tc : ndarray, size (nc)</span>
<span class="sd">        Critical temperatures (K)</span>
<span class="sd">    omega : ndarray, size (nc)</span>
<span class="sd">        Acentric factors (--)</span>
<span class="sd">    kh_0 : ndarray, size (nc)</span>
<span class="sd">        Henry&#39;s law constants at 298.15 K and 101325 Pa (kg/(m^3 Pa))</span>
<span class="sd">    neg_dH_solR : ndarray, size (nc)</span>
<span class="sd">        The negative of the enthalpies of solution / Ru (K).</span>
<span class="sd">    nu_bar : ndarray, size (nc)</span>
<span class="sd">        Partial molar volumes at infinite dilution (m^3/mol)</span>
<span class="sd">    B : ndarray, size (nc)</span>
<span class="sd">        White and Houghton (1966) pre-exponential factor (m^2/s)</span>
<span class="sd">    dE : ndarray, size (nc)</span>
<span class="sd">        Activation energy (J/mol)</span>
<span class="sd">    K_salt : Setschenow constants (m^3/mol)</span>
<span class="sd">    </span>
<span class="sd">    See Also</span>
<span class="sd">    --------</span>
<span class="sd">    chemical_properties, FluidParticle, InsolubleParticle</span>
<span class="sd">    </span>
<span class="sd">    Examples</span>
<span class="sd">    --------</span>
<span class="sd">    &gt;&gt;&gt; air = FluidMixture([&#39;nitrogen&#39;, &#39;oxygen&#39;])</span>
<span class="sd">    &gt;&gt;&gt; yk = np.array([0.79, 0.21])</span>
<span class="sd">    &gt;&gt;&gt; m = air.masses(yk)</span>
<span class="sd">    &gt;&gt;&gt; air.density(m, 273.15+10., 101325.)</span>
<span class="sd">    array([[ 1.24260911],</span>
<span class="sd">           [ 1.24260911]])</span>
<span class="sd">    </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">composition</span><span class="p">,</span> <span class="n">delta</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">user_data</span><span class="o">=</span><span class="p">{},</span> 
                 <span class="n">delta_groups</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">isair</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> 
                 <span class="n">sigma_correction</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">1.</span><span class="p">],</span> <span class="p">[</span><span class="mf">1.</span><span class="p">]])):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">FluidMixture</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        
        <span class="c1"># Check the data type of the inputs and fix if necessary</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">composition</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="n">composition</span> <span class="o">=</span> <span class="p">[</span><span class="n">composition</span><span class="p">]</span>
        
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">delta</span><span class="p">,</span> <span class="nb">float</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">delta</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="n">delta</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">atleast_2d</span><span class="p">(</span><span class="n">delta</span><span class="p">)</span>
        
        <span class="c1"># Store the input variables and some of their derived properties</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">composition</span> <span class="o">=</span> <span class="n">composition</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nc</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">composition</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">delta</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">delta</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">delta</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">delta</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">==</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">delta</span> <span class="o">=</span> <span class="n">delta</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Error: Delta wrong shape, should be (</span><span class="si">%d</span><span class="s1">, </span><span class="si">%d</span><span class="s1">)&#39;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">))</span>
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Set to np.zeros((</span><span class="si">%d</span><span class="s1">, </span><span class="si">%d</span><span class="s1">))</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">))</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">delta</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">))</span>
        
        <span class="c1"># Store all of the chemical data</span>
        <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">chem_db</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">chem_units</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">bio_db</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">bio_units</span><span class="p">)</span> <span class="o">=</span> \
            <span class="n">chemical_properties</span><span class="o">.</span><span class="n">tamoc_data</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">user_data</span> <span class="o">=</span> <span class="n">user_data</span>
        
        <span class="c1"># Initialize the chemical composition variables used in TAMOC</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">M</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">Pc</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">Tc</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">Vc</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">Tb</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">Vb</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">omega</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kh_0</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">neg_dH_solR</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nu_bar</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">B</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dE</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">K_salt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k_bio</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t_bio</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">)</span>
        
        <span class="c1"># Fill the chemical composition variables from the chem database</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">composition</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="ow">in</span> <span class="n">user_data</span><span class="p">:</span>
                <span class="c1"># Get the properties from the user-specified dataset</span>
                <span class="n">properties</span> <span class="o">=</span> <span class="n">user_data</span><span class="p">[</span><span class="n">composition</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Get the properties from the default dataset supplied with </span>
                <span class="c1"># TAMOC</span>
                <span class="k">if</span> <span class="n">composition</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">chem_db</span><span class="p">:</span>
                    <span class="n">properties</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">chem_db</span><span class="p">[</span><span class="n">composition</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                    <span class="n">properties</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bio_db</span><span class="p">[</span><span class="n">composition</span><span class="p">[</span><span class="n">i</span><span class="p">]])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">ERROR:  </span><span class="si">%s</span><span class="s1"> is not in the &#39;</span> <span class="o">%</span> <span class="n">composition</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span>
                          <span class="s1">&#39;Chemical Properties database</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
            
            <span class="c1"># Store the chemical properties in the object attributes</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">M</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;M&#39;</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">Pc</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;Pc&#39;</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">Tc</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;Tc&#39;</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">Vc</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;Vc&#39;</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">Tb</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;Tb&#39;</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;Vb&#39;</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mf">0.</span><span class="p">:</span>
                <span class="c1"># Use Tyn &amp; Calus estimate in Poling et al. (2001)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">Vb</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="mf">0.285</span> <span class="o">*</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">Vc</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="mf">1.e6</span><span class="p">)</span><span class="o">**</span><span class="mf">1.048</span><span class="p">)</span><span class="o">*</span><span class="mf">1.e-6</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">Vb</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;Vb&#39;</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">omega</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;omega&#39;</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">kh_0</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;kh_0&#39;</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">neg_dH_solR</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;-dH_solR&#39;</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;nu_bar&#39;</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mf">0.</span><span class="p">:</span>
                <span class="c1"># Use empirical equation from Jonas Gros</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">nu_bar</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.148236984</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="mf">1000.</span> <span class="o">+</span> 
                    <span class="mf">6.789136822</span><span class="p">)</span> <span class="o">/</span> <span class="mf">100.</span><span class="o">**</span><span class="mi">3</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">nu_bar</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;nu_bar&#39;</span><span class="p">]</span>
            
            <span class="k">if</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;B&#39;</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mf">0.</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">B</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mf">5.0</span> <span class="o">*</span> <span class="mf">1.e-2</span> <span class="o">/</span> <span class="mf">100.</span><span class="o">**</span><span class="mf">2.</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">B</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;B&#39;</span><span class="p">]</span>
            
            <span class="k">if</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;dE&#39;</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mf">0.</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">dE</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mf">4000.</span> <span class="o">/</span> <span class="mf">0.238846</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">dE</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;dE&#39;</span><span class="p">]</span>
            
            <span class="k">if</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;K_salt&#39;</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mf">0.</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">K_salt</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="o">-</span><span class="mf">1.345</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="mf">2799.4</span> <span class="o">*</span> 
                                 <span class="bp">self</span><span class="o">.</span><span class="n">nu_bar</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span>  <span class="mf">0.083556</span><span class="p">)</span> <span class="o">/</span> <span class="mf">1000.</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">K_salt</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;K_salt&#39;</span><span class="p">]</span>
            
            <span class="c1"># Store the biodegradation properties if they are available</span>
            <span class="k">if</span> <span class="s1">&#39;k_bio&#39;</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">k_bio</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;k_bio&#39;</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">k_bio</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.</span>
            <span class="k">if</span> <span class="s1">&#39;t_bio&#39;</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">t_bio</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">properties</span><span class="p">[</span><span class="s1">&#39;t_bio&#39;</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">t_bio</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.</span>
        
        <span class="c1"># If we are using group contribution method (Privat and Jaubert 2012) </span>
        <span class="c1"># for the binary interaction matrix, then we must import Aij and Bij</span>
        <span class="k">if</span> <span class="n">delta_groups</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">calc_delta</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">delta_groups</span> <span class="o">=</span> <span class="n">delta_groups</span>
            <span class="n">aij_file</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">(),</span> 
                       <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">),</span> <span class="s1">&#39;data&#39;</span><span class="p">)),</span><span class="s1">&#39;Aij.csv&#39;</span><span class="p">)</span>
            <span class="n">bij_file</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">(),</span> 
                       <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">),</span> <span class="s1">&#39;data&#39;</span><span class="p">)),</span><span class="s1">&#39;Bij.csv&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">Aij</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">loadtxt</span><span class="p">(</span><span class="n">aij_file</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">&#39;,&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.e6</span> <span class="c1"># Pa</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">Bij</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">loadtxt</span><span class="p">(</span><span class="n">bij_file</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">&#39;,&#39;</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.e6</span> <span class="c1"># Pa</span>
        
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">calc_delta</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">delta_groups</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">,</span> <span class="mi">15</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">Aij</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">15</span><span class="p">,</span> <span class="mi">15</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">Bij</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">15</span><span class="p">,</span> <span class="mi">15</span><span class="p">))</span>
        
        <span class="c1"># Specify that adequate information is contained in the object to </span>
        <span class="c1"># run the solubility methods</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">issoluble</span> <span class="o">=</span> <span class="kc">True</span>
        
        <span class="c1"># Ideal gas constant</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">Ru</span> <span class="o">=</span> <span class="mf">8.314510</span>  <span class="c1"># (J/(kg K))</span>
        
        <span class="c1"># Store whether or not the fluid is air</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">isair</span> <span class="o">=</span> <span class="n">isair</span>
        
        <span class="c1"># Store the interfacial tension correction factor</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sigma_correction</span> <span class="o">=</span> <span class="n">sigma_correction</span>
    
<div class="viewcode-block" id="FluidMixture.masses"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.masses.html#dbm.FluidMixture.masses">[docs]</a>    <span class="k">def</span> <span class="nf">masses</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Convert the moles of each component in a mixture to their masses (kg).</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        n : ndarray, size (nc)</span>
<span class="sd">            moles of each component in a mixture (--)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">n</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span></div>
    
<div class="viewcode-block" id="FluidMixture.mass_frac"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.mass_frac.html#dbm.FluidMixture.mass_frac">[docs]</a>    <span class="k">def</span> <span class="nf">mass_frac</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculate the mass fraction (--) from the number of moles of each </span>
<span class="sd">        component in a mixture.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        n : ndarray, size (nc)</span>
<span class="sd">            moles of each component in a mixture (--)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        mf : ndarray, size (nc)</span>
<span class="sd">            mass fractions of each component in a mixture (--)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">m</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">masses</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">m</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">m</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="FluidMixture.moles"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.moles.html#dbm.FluidMixture.moles">[docs]</a>    <span class="k">def</span> <span class="nf">moles</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Convert the masses of each component in a mixture to their moles </span>
<span class="sd">        (mol).</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        n : ndarray, size (nc)</span>
<span class="sd">            moles of each component in a mixture (--)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">m</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span></div>
    
<div class="viewcode-block" id="FluidMixture.mol_frac"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.mol_frac.html#dbm.FluidMixture.mol_frac">[docs]</a>    <span class="k">def</span> <span class="nf">mol_frac</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calcualte the mole fraction (--) from the masses of each component in </span>
<span class="sd">        a mixture.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)        </span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        yk : ndarray, size (nc)</span>
<span class="sd">            mole fraction of each component in a mixture (--)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_eos.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">mole_fraction</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="FluidMixture.partial_pressures"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.partial_pressures.html#dbm.FluidMixture.partial_pressures">[docs]</a>    <span class="k">def</span> <span class="nf">partial_pressures</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">P</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the partial pressure (Pa) of each component in a mixture.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)</span>
<span class="sd">        P : float</span>
<span class="sd">            mixture pressure (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Pk : ndarray, size (nc)</span>
<span class="sd">            partial pressures of each component in a mixture (Pa)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">yk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">mol_frac</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">P</span> <span class="o">*</span> <span class="n">yk</span></div>
    
<div class="viewcode-block" id="FluidMixture.density"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.density.html#dbm.FluidMixture.density">[docs]</a>    <span class="k">def</span> <span class="nf">density</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the gas and liquid density (kg/m^3) of a fluid mixture at the </span>
<span class="sd">        given state.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            mixture temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            mixture pressure (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        rho_p : ndarray, size (2,1)</span>
<span class="sd">            density of the gas phase (row 1) and liquid phase (row 2) of a </span>
<span class="sd">            fluid mixture (kg/m^3)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_eos.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Pc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Tc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Vc</span><span class="p">,</span> 
                             <span class="bp">self</span><span class="o">.</span><span class="n">omega</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">delta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Aij</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Bij</span><span class="p">,</span> 
                             <span class="bp">self</span><span class="o">.</span><span class="n">delta_groups</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">calc_delta</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="FluidMixture.fugacity"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.fugacity.html#dbm.FluidMixture.fugacity">[docs]</a>    <span class="k">def</span> <span class="nf">fugacity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the gas and liquid fugacity (Pa) of a fluid mixture at the </span>
<span class="sd">        given state.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            mixture temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            mixture pressure (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        fk : ndarray, size (2, nc)</span>
<span class="sd">            fugacity coefficients of the gas phase (row 1) and liquid phase</span>
<span class="sd">            (row 2) for each component in a mixture (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_eos.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">fugacity</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Pc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Tc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">omega</span><span class="p">,</span> 
                              <span class="bp">self</span><span class="o">.</span><span class="n">delta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Aij</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Bij</span><span class="p">,</span> 
                              <span class="bp">self</span><span class="o">.</span><span class="n">delta_groups</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">calc_delta</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="FluidMixture.viscosity"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.viscosity.html#dbm.FluidMixture.viscosity">[docs]</a>    <span class="k">def</span> <span class="nf">viscosity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the dynamic viscosity of the gas/liquid mixture.</span>
<span class="sd">        </span>
<span class="sd">        Computes the dynamic viscosity of gas and liquid using correlation</span>
<span class="sd">        equations in Pedersen et al. (2014) &quot;Phase Behavior of Petroleum</span>
<span class="sd">        Reservoir Fluids&quot;, 2nd edition, chapter 10.  This function has been</span>
<span class="sd">        tested for non-hydrocarbon mixtures (oxygen, carbon dioxide) and </span>
<span class="sd">        shown to give reasonable results; hence, the same equations are used</span>
<span class="sd">        for all mixtures.</span>
<span class="sd">        </span>
<span class="sd">        Parameters size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            mixture temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            mixture pressure (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        mu_p : ndarray, size (2)</span>
<span class="sd">            dynamic viscosity for gas (row 1) and liquid (row 2) (Pa s)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">viscosity</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Pc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Tc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Vc</span><span class="p">,</span> 
                               <span class="bp">self</span><span class="o">.</span><span class="n">omega</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">delta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Aij</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Bij</span><span class="p">,</span> 
                               <span class="bp">self</span><span class="o">.</span><span class="n">delta_groups</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">calc_delta</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="FluidMixture.interface_tension"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.interface_tension.html#dbm.FluidMixture.interface_tension">[docs]</a>    <span class="k">def</span> <span class="nf">interface_tension</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">S</span><span class="p">,</span> <span class="n">P</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the interfacial tension between gas/liquid and water</span>
<span class="sd">        </span>
<span class="sd">        If `air` is False (thus, assume hydrocarbon), this method computes</span>
<span class="sd">        the interfacial tension between the gas and liquid phases of the</span>
<span class="sd">        mixture and water using equations in Danesh (1998) &quot;PVT and Phase</span>
<span class="sd">        Behaviour Of Petroleum Reservoir Fluids,&quot; Chapter 8.  Otherwise, we</span>
<span class="sd">        treat the fluid like air and use the surface tension of seawater from</span>
<span class="sd">        Sharqawy et al. (2010), Table 6.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            mixture temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            mixture pressure (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        sigma_p : ndarray, size (2)</span>
<span class="sd">            interfacial tension for gas (row 1) and liquid (row 2) (N/m)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isair</span><span class="p">:</span>
            <span class="c1"># Use the surface tension of seawater with air</span>
            <span class="n">sigma_0</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">sigma</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">S</span><span class="p">)</span>
            <span class="n">sigma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="n">sigma_0</span><span class="p">],</span> <span class="p">[</span><span class="n">sigma_0</span><span class="p">]])</span>
            
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Compute the local density of water</span>
            <span class="n">rho_w</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">S</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
            
            <span class="c1"># Compute the density of the mixture phases</span>
            <span class="n">rho_p</span> <span class="o">=</span> <span class="n">FluidMixture</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
            
            <span class="c1"># Get the density difference in g/cm^3</span>
            <span class="n">delta_rho</span> <span class="o">=</span> <span class="p">(</span><span class="n">rho_w</span> <span class="o">-</span> <span class="n">rho_p</span><span class="p">)</span> <span class="o">/</span> <span class="mf">1000.</span>
            
            <span class="c1"># Compute the pseudo critical temperature using mole fractions as </span>
            <span class="c1"># weights</span>
            <span class="n">xi</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">mol_frac</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
            <span class="n">Tc</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">Tc</span> <span class="o">*</span> <span class="n">xi</span><span class="p">)</span>
            
            <span class="c1"># Get the interfacial tension</span>
            <span class="n">sigma</span> <span class="o">=</span> <span class="mf">0.111</span> <span class="o">*</span> <span class="n">delta_rho</span><span class="o">**</span><span class="mf">1.024</span> <span class="o">*</span> <span class="p">(</span><span class="n">T</span> <span class="o">/</span> <span class="n">Tc</span><span class="p">)</span><span class="o">**</span><span class="p">(</span><span class="o">-</span><span class="mf">1.25</span><span class="p">)</span>
            
            <span class="c1"># Adjust the interfacial tension to a measured value</span>
            <span class="n">sigma</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sigma_correction</span> <span class="o">*</span> <span class="n">sigma</span>
        
        <span class="c1"># Return the Interfacial tension</span>
        <span class="k">return</span> <span class="n">sigma</span></div>
    
<div class="viewcode-block" id="FluidMixture.equilibrium"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.equilibrium.html#dbm.FluidMixture.equilibrium">[docs]</a>    <span class="k">def</span> <span class="nf">equilibrium</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">K</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the equilibrium composition of a gas/liquid mixture.</span>
<span class="sd">        </span>
<span class="sd">        Computes the equilibrium composition of a gas/liquid mixture using the</span>
<span class="sd">        procedure in Michelson and Mollerup (2007) and McCain (1990).  </span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        dbm_obj : dbm.FluidMixture or dbm.FluidParticle</span>
<span class="sd">            DBM FluidMixture or FluidParticle object</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            mixture temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            mixture pressure (Pa)</span>
<span class="sd">        K : ndarray, size (nc)</span>
<span class="sd">            array of partition coefficients to use as an initial guess for</span>
<span class="sd">            K-factor.  Default is `None`, in which case the standard initial</span>
<span class="sd">            guess will be used.</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        A tuple containing:</span>
<span class="sd">        </span>
<span class="sd">            m : ndarray, size(2, nc)</span>
<span class="sd">                masses of each component in a mixture at equilibrium between </span>
<span class="sd">                the gas and liquid phases (kg)</span>
<span class="sd">            xi : ndarray, size(2, nc)</span>
<span class="sd">                mole fractions of each component in the mixture at </span>
<span class="sd">                equilibrium between the gas and liquid phases (--)</span>
<span class="sd">            K : ndarray, size(nc)</span>
<span class="sd">                partition coefficients expressed as K-factor (--)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the function equil_MM which uses the Michelsen and Mollerup </span>
<span class="sd">        (2007) procedure to find a stable solution</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Get the mole fractions and K-factors at equilibrium</span>
        <span class="n">xi</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">K</span> <span class="o">=</span> <span class="n">equil_MM</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Pc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Tc</span><span class="p">,</span>
                               <span class="bp">self</span><span class="o">.</span><span class="n">omega</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">delta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Aij</span><span class="p">,</span> 
                               <span class="bp">self</span><span class="o">.</span><span class="n">Bij</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">delta_groups</span><span class="p">,</span>
                               <span class="bp">self</span><span class="o">.</span><span class="n">calc_delta</span><span class="p">,</span> <span class="n">K</span><span class="p">)</span>
        
        <span class="c1"># Get the total moles of each molecule (both phases together)</span>
        <span class="n">n_tot</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">moles</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        
        <span class="c1"># Get the total number of moles in gas phase using the first </span>
        <span class="c1"># non-zero component in the mixture (note that this is independent of </span>
        <span class="c1"># which component you pick):</span>
        <span class="n">idx</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="n">m</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="mf">0.</span><span class="p">:</span>
            <span class="n">idx</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">ng</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">((</span><span class="n">n_tot</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">-</span> <span class="p">(</span><span class="n">xi</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="n">idx</span><span class="p">]</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">n_tot</span><span class="p">)))</span> <span class="o">/</span> 
            <span class="p">(</span><span class="n">xi</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="n">idx</span><span class="p">]</span><span class="o">-</span><span class="n">xi</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="n">idx</span><span class="p">]))</span>
        
        <span class="c1"># Get the moles of each component in gas (line 1) and liquid (line 2) </span>
        <span class="c1"># phase</span>
        <span class="n">n</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">))</span>
        <span class="n">n</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span> <span class="o">=</span> <span class="n">xi</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span> <span class="o">*</span> <span class="n">ng</span>
        <span class="n">n</span><span class="p">[</span><span class="mi">1</span><span class="p">,:]</span> <span class="o">=</span> <span class="n">xi</span><span class="p">[</span><span class="mi">1</span><span class="p">,:]</span> <span class="o">*</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">n_tot</span><span class="p">)</span> <span class="o">-</span> <span class="n">ng</span><span class="p">)</span>
        
        <span class="c1"># Finally converts to mass</span>
        <span class="n">m</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">):</span>
            <span class="n">m</span><span class="p">[</span><span class="n">i</span><span class="p">,:]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">masses</span><span class="p">(</span><span class="n">n</span><span class="p">[</span><span class="n">i</span><span class="p">,:])</span>
        
        <span class="k">return</span> <span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">xi</span><span class="p">,</span> <span class="n">K</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="FluidMixture.solubility"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.solubility.html#dbm.FluidMixture.solubility">[docs]</a>    <span class="k">def</span> <span class="nf">solubility</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the solubility (kg/m^3) of each component of a mixture in both</span>
<span class="sd">        gas and liquid dissolving into seawater.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            mixture temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            mixture pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of the ambient seawter (psu)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Cs : ndarray, size (2, nc)</span>
<span class="sd">            solubility of the gas phase (row 1) and liquid phase (row 2) for </span>
<span class="sd">            each component in a mixture (kg/m^3)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        It is assumed that the mixture is at the same pressure as the ambient </span>
<span class="sd">        seawater and that the temperature at the interface is that of the </span>
<span class="sd">        mixture.</span>

<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_eos.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Compute the Henry&#39;s law coefficients using the temperature of the</span>
        <span class="c1"># seawater</span>
        <span class="n">kh</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">kh_insitu</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">kh_0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">neg_dH_solR</span><span class="p">,</span> 
                             <span class="bp">self</span><span class="o">.</span><span class="n">nu_bar</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">K_salt</span><span class="p">)</span>
        
        <span class="c1"># Compute the mixture fugacity using the temperature of the mixture</span>
        <span class="n">f</span> <span class="o">=</span> <span class="n">FluidMixture</span><span class="o">.</span><span class="n">fugacity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        
        <span class="c1"># Get the solubility of each phase separately</span>
        <span class="n">Cs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">nc</span><span class="p">))</span>
        <span class="n">Cs</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">sw_solubility</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">,:],</span> <span class="n">kh</span><span class="p">)</span>
        <span class="n">Cs</span><span class="p">[</span><span class="mi">1</span><span class="p">,:]</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">sw_solubility</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="mi">1</span><span class="p">,:],</span> <span class="n">kh</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">Cs</span></div>
    
<div class="viewcode-block" id="FluidMixture.diffusivity"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.diffusivity.html#dbm.FluidMixture.diffusivity">[docs]</a>    <span class="k">def</span> <span class="nf">diffusivity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the diffusivity (m^2/s) of each component of a mixture into </span>
<span class="sd">        seawater at the given temperature.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        P : float</span>
<span class="sd">            pressure of ambient seawater (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        D : ndarray, size (nc)</span>
<span class="sd">            diffusion coefficients for each component of a mixture into </span>
<span class="sd">            seawater (m^2/s)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_eos.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Compute the viscosity of seawater</span>
        <span class="n">mu</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">mu</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        
        <span class="c1"># Return the diffusivities</span>
        <span class="k">return</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">diffusivity</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Vb</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="FluidMixture.hydrate_stability"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.hydrate_stability.html#dbm.FluidMixture.hydrate_stability">[docs]</a>    <span class="k">def</span> <span class="nf">hydrate_stability</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">P</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the hydrate formation temperature at the given pressure</span>
<span class="sd">        </span>
<span class="sd">        Use the K_vsi method from Sloan and Koh (2008) to compute the hydrate</span>
<span class="sd">        formation/dissociation temperature at the given pressure.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)</span>
<span class="sd">        P : float</span>
<span class="sd">            ambient pressure (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        T_hyd : float</span>
<span class="sd">            critical hydrate stability temperature (K)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        This method relys on the data fitted in Equation 4-1 in Sloan and Koh</span>
<span class="sd">        (2008), which is over a restricted range of temperature and pressure.</span>
<span class="sd">        In particular, when the pressure is outside (usually lower than) the </span>
<span class="sd">        range of data, the model can predict spurious results.</span>
<span class="sd">        </span>
<span class="sd">        TODO (S. Socolofsky, October 1, 2013):  Get the original papers and </span>
<span class="sd">        understand the limits of the range of applicability of this model.  </span>
<span class="sd">        Use this understanding to put bounds on the computation and ensure</span>
<span class="sd">        that accurate results are always returned.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Get the mole fraction of the gas components in the K_vsi model in </span>
        <span class="c1"># the order assumed in the model.</span>
        <span class="n">gases</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;methane&#39;</span><span class="p">,</span> <span class="s1">&#39;ethane&#39;</span><span class="p">,</span> <span class="s1">&#39;propane&#39;</span><span class="p">,</span> <span class="s1">&#39;isobutane&#39;</span><span class="p">,</span> <span class="s1">&#39;n-butane&#39;</span><span class="p">,</span> 
                 <span class="s1">&#39;nitrogen&#39;</span><span class="p">,</span> <span class="s1">&#39;carbon_dioxide&#39;</span><span class="p">,</span> <span class="s1">&#39;hydrogen_sulfide&#39;</span><span class="p">]</span>
        <span class="n">m_gases</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">gases</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">gases</span><span class="p">)):</span>
            <span class="k">if</span> <span class="n">gases</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">composition</span><span class="p">:</span>
                <span class="n">m_gases</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">m</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">gases</span><span class="p">[</span><span class="n">i</span><span class="p">])]</span>
        
        <span class="k">def</span> <span class="nf">residual</span><span class="p">(</span><span class="n">T_hyd</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            Compute the residual of sum (yi / K_vsi) - 1.</span>
<span class="sd">            </span>
<span class="sd">            Computes the residual of Equation 4-3 in Sloan and Koh for use</span>
<span class="sd">            in obtaining the critical formation temperature using a root-</span>
<span class="sd">            finding algorithm.</span>
<span class="sd">            </span>
<span class="sd">            Parameters</span>
<span class="sd">            ----------</span>
<span class="sd">            T_hyd : float</span>
<span class="sd">                current guess for the hydrate formation temperature (K)</span>
<span class="sd">            </span>
<span class="sd">            Returns</span>
<span class="sd">            -------</span>
<span class="sd">            res : float</span>
<span class="sd">                difference between sum (yi / K_vsi) - 1.</span>
<span class="sd">            </span>
<span class="sd">            &quot;&quot;&quot;</span>
            <span class="c1"># Get the current value of the partition coefficients</span>
            <span class="p">(</span><span class="n">K_vsi</span><span class="p">,</span> <span class="n">yk</span><span class="p">)</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">kvsi_hydrate</span><span class="p">(</span><span class="n">T_hyd</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">m_gases</span><span class="p">)</span>
            
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">yk</span> <span class="o">/</span> <span class="n">K_vsi</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
        
        <span class="c1"># Use root-finding to get the critical formation pressure</span>
        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">m_gases</span><span class="p">)</span> <span class="o">==</span> <span class="mf">0.</span><span class="p">:</span>
            <span class="c1"># Set T_hyd to zero. In this way, no ocean temperatures will be</span>
            <span class="c1"># found to be below the hydrate stability temperature, which is</span>
            <span class="c1"># the desired behavior since there are not hydrate forming</span>
            <span class="c1"># compounds in the mixture.</span>
            <span class="n">T_hyd</span> <span class="o">=</span> <span class="mf">0.</span>
            
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Use the K_vsi method to find the hydrate stability temperature</span>
            <span class="n">T_hyd</span> <span class="o">=</span> <span class="n">fsolve</span><span class="p">(</span><span class="n">residual</span><span class="p">,</span> <span class="mf">260.</span><span class="p">)</span>
        
        <span class="c1"># Return the formation temperature</span>
        <span class="k">return</span> <span class="n">T_hyd</span></div>

<div class="viewcode-block" id="FluidMixture.biodegradation_rate"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidMixture.biodegradation_rate.html#dbm.FluidMixture.biodegradation_rate">[docs]</a>    <span class="k">def</span> <span class="nf">biodegradation_rate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">lag_time</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Determine the biodegradation rate constant</span>
<span class="sd">        </span>
<span class="sd">        Returns the first-order biodegradation rate constant after the </span>
<span class="sd">        simulation time exceeds the bacterial community response lag time.  </span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        t : float</span>
<span class="sd">            current simulation time (s)</span>
<span class="sd">        lag_time : bool, default = True</span>
<span class="sd">            flag indicating whether the biodegradation rates should include</span>
<span class="sd">            a lag time (True) or not (False).  Default value is True.</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        k_bio : ndarray, size (nc)</span>
<span class="sd">            first-order biodegradation rate constants (1/s)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        The user provides both the first-order rate constants and the </span>
<span class="sd">        constant lag times in the `user_data` provided to the model or</span>
<span class="sd">        using the results in the `TAMOC` dataset ``./data/BioData.csv``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Determine the correct values of the rate constant</span>
        <span class="n">k_bio</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_bio</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">lag_time</span><span class="p">:</span>
            <span class="n">k_bio</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">t_bio</span> <span class="o">&gt;</span> <span class="n">t</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.</span>
        
        <span class="k">return</span> <span class="n">k_bio</span></div></div>


<div class="viewcode-block" id="FluidParticle"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.html#dbm.FluidParticle">[docs]</a><span class="k">class</span> <span class="nc">FluidParticle</span><span class="p">(</span><span class="n">FluidMixture</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Class object for a soluble fluid particle</span>
<span class="sd">    </span>
<span class="sd">    This object defines the behavior of a soluble fluid particle.  The object</span>
<span class="sd">    inherits the internal variables and methods from the `FluidMixture` </span>
<span class="sd">    object, but limits the output to a single phase, defined by the internal </span>
<span class="sd">    variable `fp_type`.  It further extends the `FluidMixture` class to </span>
<span class="sd">    include the properties inherent to particles (e.g., shape, diameter, slip </span>
<span class="sd">    velocity, etc.).  </span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    composition : string list, length nc</span>
<span class="sd">        Contains the names of the chemical components in the mixture</span>
<span class="sd">        using the same key names as in ./data/ChemData.csv</span>
<span class="sd">    fp_type : integer</span>
<span class="sd">        Defines the fluid type (0 = gas, 1 = liquid) that is expected to be </span>
<span class="sd">        contained in the bubble.  This is needed because the Peng-Robinson</span>
<span class="sd">        equation of state returns values for both phases of a mixture.  This</span>
<span class="sd">        variable allows the class to automatically return the values for the</span>
<span class="sd">        desired phase.</span>
<span class="sd">    delta : ndarray, size (nc, nc)</span>
<span class="sd">        Binary interaction coefficients for the Peng-Robinson equation of </span>
<span class="sd">        state.  If not passed at instantiation, Python will assume a </span>
<span class="sd">        full matrix of zeros.</span>
<span class="sd">    user_data : dict</span>
<span class="sd">        A dictionary of chemical property data.  If not specified, the data</span>
<span class="sd">        loaded from `/tamoc/data/ChemData.csv` by ``chemical_properties`` will</span>
<span class="sd">        be used.  To load a different properties database, use the </span>
<span class="sd">        ``chemical_properties.load_data`` function to load in a custom </span>
<span class="sd">        database, and pass that data to this object as `user_data`.</span>
<span class="sd">    delta_groups : ndarray (nc, 15)</span>
<span class="sd">        Provides the group contribution numbers (normalized) for each </span>
<span class="sd">        component in the mixture for the 15 groups used by the Privat and</span>
<span class="sd">        Jaubert (2012) group contribution method for binary interaction </span>
<span class="sd">        coefficients.  Default is None, in which case the values in `delta`</span>
<span class="sd">        will be used.</span>
<span class="sd">    isair : bool</span>
<span class="sd">        Flag indicating whether or not fluid is air.  The methods for </span>
<span class="sd">        viscosity and interfacial tension below use correlations developed</span>
<span class="sd">        for hydocarbons.  If `isair` is False (default value), these built</span>
<span class="sd">        in methods are used.  If `isair` is True, then these methods are </span>
<span class="sd">        replaced with correlations between air and seawater.</span>
<span class="sd">    sigma_correction : float</span>
<span class="sd">        Correction factor to adjust the interfacial tension value supplied by</span>
<span class="sd">        the default model to a value measured for the mixture of interest.</span>
<span class="sd">        The correction factor should be computed as sigma_measured / </span>
<span class="sd">        sigma_model at a single P and T value.  For the FluidParticle class, </span>
<span class="sd">        sigma_correction is a scalar applied to the phase defined by </span>
<span class="sd">        fp_type.</span>
<span class="sd">    </span>
<span class="sd">    Notes</span>
<span class="sd">    -----</span>
<span class="sd">    The attributes are identical to those defined for a `FluidMixture`</span>
<span class="sd">    </span>
<span class="sd">    See Also</span>
<span class="sd">    --------</span>
<span class="sd">    FluidMixture, chemical_properties, InsolubleParticle</span>
<span class="sd">    </span>
<span class="sd">    Examples</span>
<span class="sd">    --------</span>
<span class="sd">    &gt;&gt;&gt; bub = FluidParticle([&#39;nitrogen&#39;, &#39;oxygen&#39;], fp_type=0)</span>
<span class="sd">    &gt;&gt;&gt; yk = np.array([0.79, 0.21])</span>
<span class="sd">    &gt;&gt;&gt; T = 273.15 + 30.</span>
<span class="sd">    &gt;&gt;&gt; P = 10.e5</span>
<span class="sd">    &gt;&gt;&gt; Sa = 35.</span>
<span class="sd">    &gt;&gt;&gt; Ta = 273.15 + 20.</span>
<span class="sd">    &gt;&gt;&gt; m = bub.masses_by_diameter(0.01, T, P, yk)</span>
<span class="sd">    array([  4.61873994e-06,   1.40243772e-06])</span>
<span class="sd">    &gt;&gt;&gt; bub.density(m, T, P)</span>
<span class="sd">    11.499602249012074</span>
<span class="sd">    &gt;&gt;&gt; bub.slip_velocity(m, T, P, Sa, Ta)</span>
<span class="sd">    0.22197023589052</span>
<span class="sd">    </span>
<span class="sd">    &quot;&quot;&quot;</span>
<div class="viewcode-block" id="FluidParticle.__init__"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.html#dbm.FluidParticle.__init__">[docs]</a>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">composition</span><span class="p">,</span> <span class="n">fp_type</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">delta</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">user_data</span><span class="o">=</span><span class="p">{},</span>
                 <span class="n">delta_groups</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">isair</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">sigma_correction</span><span class="o">=</span><span class="mf">1.</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">FluidParticle</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">composition</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> <span class="n">user_data</span><span class="p">,</span>
                                            <span class="n">delta_groups</span><span class="p">,</span> <span class="n">isair</span><span class="p">,</span> 
                                            <span class="n">sigma_correction</span><span class="p">)</span>
        
        <span class="c1"># Store the input variables</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fp_type</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="FluidParticle.density"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.density.html#dbm.FluidParticle.density">[docs]</a>    <span class="k">def</span> <span class="nf">density</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the particle density (kg/m^3) of the fluid in the phase given </span>
<span class="sd">        by `fp_type`.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in the particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        rho_p : float</span>
<span class="sd">            density of the fluid particle (kg/m^3)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the density method in the `FluidMixture` object, but only returns</span>
<span class="sd">        the value for the phase given by `fp_type`.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">FluidMixture</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">)[</span><span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span></div>
    
<div class="viewcode-block" id="FluidParticle.fugacity"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.fugacity.html#dbm.FluidParticle.fugacity">[docs]</a>    <span class="k">def</span> <span class="nf">fugacity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the particle fugacities (Pa) of the fluid in the phase given </span>
<span class="sd">        by `fp_type`.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        fk : ndarray, size (nc)</span>
<span class="sd">            fugacities of each component of the fluid particle (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the fugacity method in the `FluidMixture` object, but only </span>
<span class="sd">        returns the values for the phase given by `fp_type`.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">FluidMixture</span><span class="o">.</span><span class="n">fugacity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">)[</span><span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="p">:]</span></div>
    
<div class="viewcode-block" id="FluidParticle.viscosity"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.viscosity.html#dbm.FluidParticle.viscosity">[docs]</a>    <span class="k">def</span> <span class="nf">viscosity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the dynamic viscosity of the fluid in the phase given by </span>
<span class="sd">        `fp_type`</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a mixture (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            mixture temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            mixture pressure (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        mu_p : float</span>
<span class="sd">            dynamic viscosity (Pa s)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the density method in the `FluidMixture` object, but only returns</span>
<span class="sd">        the value for the phase given by `fp_type`.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">FluidMixture</span><span class="o">.</span><span class="n">viscosity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">)[</span><span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span></div>
    
<div class="viewcode-block" id="FluidParticle.interface_tension"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.interface_tension.html#dbm.FluidParticle.interface_tension">[docs]</a>    <span class="k">def</span> <span class="nf">interface_tension</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">S</span><span class="p">,</span> <span class="n">P</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the interfacial tension between the particle and water</span>
<span class="sd">        </span>
<span class="sd">        Computes the interfacial tension between the particle and water.  This</span>
<span class="sd">        method uses equations in Danesh (1998).</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in the particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        S : float</span>
<span class="sd">            salinity of the ambient seawter (psu)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        sigma_p : float</span>
<span class="sd">            interfacial tension (N/m)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the density method in the `FluidMixture` object, but only returns</span>
<span class="sd">        the value for the phase given by `fp_type`.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">FluidMixture</span><span class="o">.</span><span class="n">interface_tension</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">S</span><span class="p">,</span> <span class="n">P</span><span class="p">)[</span><span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span></div>
    
<div class="viewcode-block" id="FluidParticle.solubility"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.solubility.html#dbm.FluidParticle.solubility">[docs]</a>    <span class="k">def</span> <span class="nf">solubility</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the solubility (kg/m^3) of each component of a particle into </span>
<span class="sd">        seawater for the phase given by `fp_type`.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        S : float</span>
<span class="sd">            salinity of the ambient seawter (psu)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Cs : ndarray, size (nc)</span>
<span class="sd">            solubilities of each component of the fluid particle into </span>
<span class="sd">            seawater (kg/m^3)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        It is assumed that the mixture is at the same pressure as the ambient </span>
<span class="sd">        seawater and that the temperature at the interface is that of the </span>
<span class="sd">        particle.</span>
<span class="sd">        </span>
<span class="sd">        Uses the solubility method in the `FluidMixture` object, but only </span>
<span class="sd">        returns the values for the phase given by `fp_type`.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">FluidMixture</span><span class="o">.</span><span class="n">solubility</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">)[</span><span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="p">:]</span></div>
    
<div class="viewcode-block" id="FluidParticle.masses_by_diameter"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.masses_by_diameter.html#dbm.FluidParticle.masses_by_diameter">[docs]</a>    <span class="k">def</span> <span class="nf">masses_by_diameter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">yk</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find the masses (kg) of each component in a particle with equivalent </span>
<span class="sd">        spherical diameter `de` and mole fractions `yk`.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        de : float</span>
<span class="sd">            equivalent spherical diameter (m)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        yk : ndarray, size (nc)</span>
<span class="sd">            mole fractions of each component in the particle (--)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a fluid particle (kg)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">yk</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="n">yk</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">yk</span><span class="p">)</span>
        
        <span class="c1"># Get the masses for one mole of fluid</span>
        <span class="n">m</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">masses</span><span class="p">(</span><span class="n">yk</span><span class="p">)</span>
        
        <span class="c1"># Compute the actual total mass of the fluid particle using the </span>
        <span class="c1"># density computed from one mole of fluid</span>
        <span class="n">m_tot</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mf">6.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">de</span><span class="o">**</span><span class="mi">3</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        
        <span class="c1"># Determine the number of moles in the fluid particle</span>
        <span class="n">n</span> <span class="o">=</span> <span class="n">yk</span> <span class="o">*</span> <span class="n">m_tot</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">masses</span><span class="p">(</span><span class="n">n</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="FluidParticle.diameter"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.diameter.html#dbm.FluidParticle.diameter">[docs]</a>    <span class="k">def</span> <span class="nf">diameter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the equivalent spherical diameter (m) of a single fluid </span>
<span class="sd">        particle.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        de : float</span>
<span class="sd">            equivalent spherical diameter of a fluid particle (m)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="mf">6.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">)))</span><span class="o">**</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="mf">3.0</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="FluidParticle.particle_shape"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.particle_shape.html#dbm.FluidParticle.particle_shape">[docs]</a>    <span class="k">def</span> <span class="nf">particle_shape</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Determine the shape of a fluid particle from the properties of the </span>
<span class="sd">        particle and surrounding fluid.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        A tuple containing:</span>
<span class="sd">            </span>
<span class="sd">            shape : integer</span>
<span class="sd">                1 - sphere, 2 - ellipsoid, 3 - spherical cap</span>
<span class="sd">            de : float</span>
<span class="sd">                equivalent spherical diameter (m)</span>
<span class="sd">            rho_p : float</span>
<span class="sd">                particle density (kg/m^3)</span>
<span class="sd">            rho : float</span>
<span class="sd">                ambient seawater density (kg/m^3)</span>
<span class="sd">            mu : float</span>
<span class="sd">                ambient seawater dynamic viscosity (Pa s)</span>
<span class="sd">            mu_p : float</span>
<span class="sd">                dispersed phase dynamic viscosity (Pa s)</span>
<span class="sd">            sigma : float</span>
<span class="sd">                interfacial tension (N/m)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        As for the solubility calculation, we use the particle temperature to </span>
<span class="sd">        calculate properties at the interface (e.g., to calculate the </span>
<span class="sd">        interfacial tension) and the ambient temperature for properties of </span>
<span class="sd">        the bulk continuous phase (e.g., density and viscosity).</span>
<span class="sd">        </span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_phys.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Compute the fluid particle and ambient properties</span>
        <span class="n">de</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">diameter</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">rho_p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">rho</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">mu</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">mu</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">mu_p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">viscosity</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">sigma</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">interface_tension</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        
        <span class="n">shape</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">particle_shape</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">)</span>
        
        <span class="k">return</span> <span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="FluidParticle.slip_velocity"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.slip_velocity.html#dbm.FluidParticle.slip_velocity">[docs]</a>    <span class="k">def</span> <span class="nf">slip_velocity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">,</span> <span class="n">status</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the slip velocity (m/s) of a fluid particle.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        status : int</span>
<span class="sd">            flag indicating whether the particle is clean (status = 1) or</span>
<span class="sd">            dirty (status = -1).  Default value is -1.</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        us : float</span>
<span class="sd">            slip velocity of the fluid particle (m/s)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_phys.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Get the particle properties</span>
        <span class="n">shape</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span> <span class="o">=</span> \
             <span class="bp">self</span><span class="o">.</span><span class="n">particle_shape</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span>
        
        <span class="k">if</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">us</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">us_sphere</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">us</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">us_ellipsoid</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">us</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">us_spherical_cap</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">)</span>
        
        <span class="k">return</span> <span class="n">us</span></div>
    
<div class="viewcode-block" id="FluidParticle.surface_area"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.surface_area.html#dbm.FluidParticle.surface_area">[docs]</a>    <span class="k">def</span> <span class="nf">surface_area</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the surface area (m^2) of a fluid particle.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        A : float</span>
<span class="sd">            surface area of the fluid particle (m^2)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_eos.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Get the particle properties</span>
        <span class="n">shape</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span> <span class="o">=</span> \
             <span class="bp">self</span><span class="o">.</span><span class="n">particle_shape</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span>
        
        <span class="k">if</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
            <span class="c1"># Compute the surface area of a spherical cap bubble</span>
            <span class="n">us</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">slip_velocity</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span>
            <span class="n">theta_w</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">theta_w_sc</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">)</span>
            <span class="n">A</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">surface_area_sc</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">theta_w</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Compute the area of the equivalent sphere:</span>
            <span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">de</span><span class="o">**</span><span class="mi">2</span>
        
        <span class="k">return</span> <span class="n">A</span></div>
    
<div class="viewcode-block" id="FluidParticle.mass_transfer"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.mass_transfer.html#dbm.FluidParticle.mass_transfer">[docs]</a>    <span class="k">def</span> <span class="nf">mass_transfer</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">,</span> <span class="n">status</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the mass transfer coefficients (m/s) for each component in a </span>
<span class="sd">        fluid particle</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        status : int</span>
<span class="sd">            flag indicating whether the particle is clean (status = 1) or</span>
<span class="sd">            dirty (status = -1).  Default value is -1.</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        beta : ndarray, size (nc)</span>
<span class="sd">            mass transfer coefficient for each component in a fluid particle</span>
<span class="sd">            (m/s)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_phys.f95``.  This method</span>
<span class="sd">        checks for hydrate stability and returns a reduced mass transfer </span>
<span class="sd">        coefficient when hydrate shells are predicted to be present.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Get the particle properties</span>
        <span class="n">shape</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span> <span class="o">=</span> \
             <span class="bp">self</span><span class="o">.</span><span class="n">particle_shape</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span>
        
        <span class="c1"># Compute the slip velocity</span>
        <span class="n">us</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">slip_velocity</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        
        <span class="c1"># Get the diffusivities</span>
        <span class="n">D</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">diffusivity</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        
        <span class="c1"># Compute the appropriate mass transfer coefficients</span>
        <span class="k">if</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_sphere</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> 
                                     <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_ellipsoid</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> 
                                        <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_spherical_cap</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">beta</span></div>
    
<div class="viewcode-block" id="FluidParticle.heat_transfer"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.heat_transfer.html#dbm.FluidParticle.heat_transfer">[docs]</a>    <span class="k">def</span> <span class="nf">heat_transfer</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">,</span> <span class="n">status</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the heat transfer coefficient (m/s) for a fluid particle</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        status : int</span>
<span class="sd">            flag indicating whether the particle is clean (status = 1) or</span>
<span class="sd">            dirty (status = -1).  Default value is -1.</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        beta_T : float</span>
<span class="sd">            heat transfer coefficient for a fluid particle (m/s)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_eos.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Get the particle properties</span>
        <span class="n">shape</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span> <span class="o">=</span> \
             <span class="bp">self</span><span class="o">.</span><span class="n">particle_shape</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span>
        
        <span class="c1"># Get the thermal conductivity of seawater</span>
        <span class="n">k</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">k</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">seawater</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span> <span class="o">*</span> 
            <span class="n">seawater</span><span class="o">.</span><span class="n">cp</span><span class="p">())</span>
        
        <span class="c1"># Compute the slip velocity</span>
        <span class="n">us</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">slip_velocity</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        
        <span class="c1"># Compute the appropriate heat transfer coefficients.  Assume the </span>
        <span class="c1"># heat transfer has the same form as the mass transfer with the </span>
        <span class="c1"># diffusivity replaced by the thermal conductivity</span>
        <span class="k">if</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_sphere</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> 
                                     <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_ellipsoid</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> 
                                        <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_spherical_cap</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        
        <span class="k">return</span> <span class="n">beta</span></div>
    
<div class="viewcode-block" id="FluidParticle.return_all"><a class="viewcode-back" href="../autodoc/dbm/dbm.FluidParticle.return_all.html#dbm.FluidParticle.return_all">[docs]</a>    <span class="k">def</span> <span class="nf">return_all</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">,</span> <span class="n">status</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute all of the dynamic properties of the bubble in an efficient</span>
<span class="sd">        manner (e.g., minimizing replicate calls to functions).</span>
<span class="sd">        </span>
<span class="sd">        This method repeats the calculations in the individual property </span>
<span class="sd">        methods, and does not call the methods already defined.  This is done</span>
<span class="sd">        so that multiple calls to functions (e.g., slip velocity) do not </span>
<span class="sd">        occur.  </span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : ndarray, size (nc)</span>
<span class="sd">            masses of each component in a particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        status : int</span>
<span class="sd">            flag indicating whether the particle is clean (status = 1) or</span>
<span class="sd">            dirty (status = -1).  Default value is -1.</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        A tuple containing:</span>
<span class="sd">            </span>
<span class="sd">            shape : integer</span>
<span class="sd">                1 - sphere, 2 - ellipsoid, 3 - spherical cap</span>
<span class="sd">            de : float</span>
<span class="sd">                equivalent spherical diameter (m)</span>
<span class="sd">            rho_p : float</span>
<span class="sd">                particle density (kg/m^3)</span>
<span class="sd">            us : float</span>
<span class="sd">                slip velocity (m/s)</span>
<span class="sd">            A : float </span>
<span class="sd">                surface area (m^2)</span>
<span class="sd">            Cs : ndarray, size (nc)</span>
<span class="sd">                solubility (kg/m^3)</span>
<span class="sd">            beta : ndarray, size (nc)</span>
<span class="sd">                mass transfer coefficient (m/s)</span>
<span class="sd">            beta_T : float</span>
<span class="sd">                heat transfer coefficient (m/s)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_eos.f95``.  This method</span>
<span class="sd">        checks for hydrate stability and returns a reduced mass transfer </span>
<span class="sd">        coefficient when hydrate shells are predicted to be present.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Ambient properties of seawater</span>
        <span class="n">rho</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">mu</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">mu</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">sigma</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">interface_tension</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">D</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">diffusivity</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Vb</span><span class="p">)</span>
        <span class="n">k</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">k</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">rho</span> <span class="o">*</span> <span class="n">seawater</span><span class="o">.</span><span class="n">cp</span><span class="p">())</span>
        
        <span class="c1"># Particle density, equivalent diameter and shape</span>
        <span class="n">rho_p</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Pc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Tc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Vc</span><span class="p">,</span> 
                              <span class="bp">self</span><span class="o">.</span><span class="n">omega</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">delta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Aij</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Bij</span><span class="p">,</span> 
                              <span class="bp">self</span><span class="o">.</span><span class="n">delta_groups</span><span class="p">,</span> 
                              <span class="bp">self</span><span class="o">.</span><span class="n">calc_delta</span><span class="p">)[</span><span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
        <span class="n">de</span> <span class="o">=</span> <span class="p">(</span><span class="mf">6.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">rho_p</span><span class="p">))</span><span class="o">**</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="mf">3.0</span><span class="p">)</span>
        <span class="n">shape</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">particle_shape</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">)</span>
        
        <span class="c1"># Other particle properties</span>
        <span class="n">mu_p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">viscosity</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
                
        <span class="c1"># Solubility</span>
        <span class="n">f</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">fugacity</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Pc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Tc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">omega</span><span class="p">,</span> 
                           <span class="bp">self</span><span class="o">.</span><span class="n">delta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Aij</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">Bij</span><span class="p">,</span> 
                           <span class="bp">self</span><span class="o">.</span><span class="n">delta_groups</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">calc_delta</span><span class="p">)</span>
        <span class="n">kh</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">kh_insitu</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">kh_0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">neg_dH_solR</span><span class="p">,</span> 
                             <span class="bp">self</span><span class="o">.</span><span class="n">nu_bar</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">M</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">K_salt</span><span class="p">)</span>
        <span class="n">Cs</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">sw_solubility</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,:],</span> <span class="n">kh</span><span class="p">)</span>
        <span class="n">K_hyd</span> <span class="o">=</span> <span class="mf">1.0</span>
        
        <span class="c1"># Shape-specific properties</span>
        <span class="k">if</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">us</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">us_sphere</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">)</span>
            <span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">de</span><span class="o">**</span><span class="mi">2</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_sphere</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> 
                                     <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
            <span class="n">beta_T</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_sphere</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> 
                                       <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="n">status</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">us</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">us_ellipsoid</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
            <span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">de</span><span class="o">**</span><span class="mi">2</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_ellipsoid</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> 
                                        <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
            <span class="n">beta_T</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_ellipsoid</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> 
                                          <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="n">status</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">us</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">us_spherical_cap</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">)</span>
            <span class="n">theta_w</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">theta_w_sc</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">)</span>
            <span class="n">A</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">surface_area_sc</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">theta_w</span><span class="p">)</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_spherical_cap</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">D</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
            <span class="n">beta_T</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_spherical_cap</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> 
                                              <span class="n">status</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        
        <span class="k">return</span> <span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="n">Cs</span><span class="p">,</span> <span class="n">K_hyd</span> <span class="o">*</span> <span class="n">beta</span><span class="p">,</span> <span class="n">beta_T</span><span class="p">)</span></div></div>
    

<div class="viewcode-block" id="InsolubleParticle"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.html#dbm.InsolubleParticle">[docs]</a><span class="k">class</span> <span class="nc">InsolubleParticle</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Class object for an insoluble (inert) fluid particle</span>
<span class="sd">    </span>
<span class="sd">    This object defines the behavior of an inert fluid particle.  The purpose</span>
<span class="sd">    of this class is to simulate particle that cannot be described by the </span>
<span class="sd">    Peng-Robinson equation of state, such as sand, or that do not require</span>
<span class="sd">    such computational expense, such as dead oil.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    isfluid : logical</span>
<span class="sd">        `True` or `False`; states whether or not the inert particle could have </span>
<span class="sd">        a mobile interface.  For example, choose `True` for oil and `False` </span>
<span class="sd">        for sand.</span>
<span class="sd">    iscompressible : logical</span>
<span class="sd">        `True` or `False`; selects the equation of state for density.  `True` </span>
<span class="sd">        uses the API gravity, isothermal compression and isobaric thermal </span>
<span class="sd">        expansion; whereas, `False` returns the constant density specified at </span>
<span class="sd">        instantiation.</span>
<span class="sd">    rho_p : float</span>
<span class="sd">        particle density (default value is 930 kg/m^3)</span>
<span class="sd">    gamma : float</span>
<span class="sd">        API gravity (default value is 30 deg API)</span>
<span class="sd">    beta : float</span>
<span class="sd">        thermal expansion coefficient (default value is 0.0007 K^(-1))</span>
<span class="sd">    co : float</span>
<span class="sd">        isothermal compressibility coefficient (default value is </span>
<span class="sd">        2.90075e-9 Pa^(-1))</span>
<span class="sd">    k_bio : float</span>
<span class="sd">        first-order biodegradation rate constant (1/s)</span>
<span class="sd">    t_bio : float</span>
<span class="sd">        lag time before onset of first-order biodegradation (s)</span>
<span class="sd">    fp_type : integer</span>
<span class="sd">        Defines the fluid type (0 = gas, 1 = liquid) that is expected to be </span>
<span class="sd">        contained in the particle.  This is needed because the heat transfer</span>
<span class="sd">        equations are different for gas and liquid.  The default value is 1.</span>
<span class="sd">    </span>
<span class="sd">    Attributes</span>
<span class="sd">    ----------</span>
<span class="sd">    composition : string list</span>
<span class="sd">        Set equal to [&#39;inert&#39;]</span>
<span class="sd">    nc : integer</span>
<span class="sd">        Number of components, set equal to 1</span>
<span class="sd">    issoluble : logical, False</span>
<span class="sd">        Indicates the particle is not soluble</span>
<span class="sd">    </span>
<span class="sd">    See Also</span>
<span class="sd">    --------</span>
<span class="sd">    FluidMixture, chemical_properties, FluidParticle</span>
<span class="sd">    </span>
<span class="sd">    Examples</span>
<span class="sd">    --------</span>
<span class="sd">    &gt;&gt;&gt; oil = InsolubleParticle(True, True)</span>
<span class="sd">    &gt;&gt;&gt; T = 273.15 + 30.</span>
<span class="sd">    &gt;&gt;&gt; P = 10.e5</span>
<span class="sd">    &gt;&gt;&gt; Sa = 35.</span>
<span class="sd">    &gt;&gt;&gt; Ta = 273.15 + 20.</span>
<span class="sd">    &gt;&gt;&gt; m = oil.mass_by_diameter(0.01, T, P, Sa, Ta)</span>
<span class="sd">    0.00045487710681354078</span>
<span class="sd">    &gt;&gt;&gt; oil.density(T, P, Sa, Ta)</span>
<span class="sd">    868.75128058458085</span>
<span class="sd">    &gt;&gt;&gt; oil.slip_velocity(m, T, P, Sa, Ta)</span>
<span class="sd">    0.14332887200025926</span>
<span class="sd">    </span>
<span class="sd">    &quot;&quot;&quot;</span>
<div class="viewcode-block" id="InsolubleParticle.__init__"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.html#dbm.InsolubleParticle.__init__">[docs]</a>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">isfluid</span><span class="p">,</span> <span class="n">iscompressible</span><span class="p">,</span> <span class="n">rho_p</span><span class="o">=</span><span class="mf">930.</span><span class="p">,</span> <span class="n">gamma</span><span class="o">=</span><span class="mf">30.</span><span class="p">,</span> 
                 <span class="n">beta</span><span class="o">=</span><span class="mf">0.0007</span><span class="p">,</span> <span class="n">co</span><span class="o">=</span><span class="mf">2.90075e-9</span><span class="p">,</span> <span class="n">k_bio</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span> <span class="n">t_bio</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span>
                 <span class="n">fp_type</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">InsolubleParticle</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        
        <span class="c1"># Store the input variables</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">isfluid</span> <span class="o">=</span> <span class="n">isfluid</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">iscompressible</span> <span class="o">=</span> <span class="n">iscompressible</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rho_p</span> <span class="o">=</span> <span class="n">rho_p</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gamma</span> <span class="o">=</span> <span class="n">gamma</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">beta</span> <span class="o">=</span> <span class="n">beta</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">co</span> <span class="o">=</span> <span class="n">co</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">k_bio</span> <span class="o">=</span> <span class="n">k_bio</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t_bio</span> <span class="o">=</span> <span class="n">t_bio</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fp_type</span><span class="p">)</span>
        
        <span class="c1"># Specify that the particle is not soluble and is therefore treated</span>
        <span class="c1"># like a single substance and store whether or not the fluid is </span>
        <span class="c1"># like air</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">issoluble</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nc</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">composition</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;inert&#39;</span><span class="p">]</span></div>
    
<div class="viewcode-block" id="InsolubleParticle.density"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.density.html#dbm.InsolubleParticle.density">[docs]</a>    <span class="k">def</span> <span class="nf">density</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the density (kg/m^3) of an inert fluid particle.</span>
<span class="sd">        </span>
<span class="sd">        If the particle is compressible, this method computes the particle </span>
<span class="sd">        density following McCain (1990), using the API gravity (ee pages 224 </span>
<span class="sd">        and following in McCain).  This would be typical of an oil.</span>
<span class="sd">        </span>
<span class="sd">        Otherwise, the method returns the constant density stored in the </span>
<span class="sd">        internal variable `rho_p`.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        rho_p : float</span>
<span class="sd">            density of the inert particle (kg/m^3)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">iscompressible</span><span class="p">:</span>
            
            <span class="c1"># Set the standard conditions for the API gravity</span>
            <span class="n">P_stp</span> <span class="o">=</span> <span class="mf">101325.0</span>
            <span class="n">T_stp</span> <span class="o">=</span> <span class="mf">273.15</span> <span class="o">+</span> <span class="p">(</span><span class="mf">60.0</span> <span class="o">-</span> <span class="mf">32.0</span><span class="p">)</span> <span class="o">*</span> <span class="mf">5.0</span> <span class="o">/</span> <span class="mf">9.0</span>
            
            <span class="c1"># Get the density of water at standard conditions</span>
            <span class="n">rho_stp</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">T_stp</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="n">P_stp</span><span class="p">)</span>
            
            <span class="c1"># Use the API gravity equation (8-2) in McCain (1990) to get the</span>
            <span class="c1"># fluid particle density at standard conditions</span>
            <span class="n">gamma_0</span> <span class="o">=</span> <span class="mf">141.5</span> <span class="o">/</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">gamma</span> <span class="o">+</span> <span class="mf">131.5</span><span class="p">)</span>
            <span class="n">rho_p</span> <span class="o">=</span> <span class="n">gamma_0</span> <span class="o">*</span> <span class="n">rho_stp</span>
            
            <span class="c1"># Isothermal compression to in-situ pressure using equation (8-19) </span>
            <span class="c1"># in McCain (1990).</span>
            <span class="n">rho_p</span> <span class="o">=</span> <span class="n">rho_p</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">co</span> <span class="o">*</span> <span class="p">(</span><span class="n">P</span> <span class="o">-</span> <span class="n">P_stp</span><span class="p">))</span>
            
            <span class="c1"># Isobaric compression to oil temperature using equation (8-28) in</span>
            <span class="c1"># McCain (1990).</span>
            <span class="n">rho_p</span> <span class="o">=</span> <span class="n">rho_p</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">beta</span> <span class="o">*</span> <span class="p">(</span><span class="n">T</span> <span class="o">-</span> <span class="n">T_stp</span><span class="p">))</span>
        
        <span class="k">else</span><span class="p">:</span>
            <span class="n">rho_p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rho_p</span>
        
        <span class="k">return</span> <span class="n">rho_p</span></div>
    
<div class="viewcode-block" id="InsolubleParticle.viscosity"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.viscosity.html#dbm.InsolubleParticle.viscosity">[docs]</a>    <span class="k">def</span> <span class="nf">viscosity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">T</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the dynamic viscosity of the liquid if applicable.</span>
<span class="sd">        </span>
<span class="sd">        Computes the dynamic viscosity of gas and liquid using correlation </span>
<span class="sd">        equations in McCain (1990).  This is the only method we can use</span>
<span class="sd">        since an `InsolubleParticle` has not `composition`.  If solid, the</span>
<span class="sd">        viscosity is returned as infinite.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        T : float</span>
<span class="sd">            mixture temperature (K)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        mu_p : ndarray, size (2)</span>
<span class="sd">            dynamic viscosity (Pa s)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isfluid</span><span class="p">:</span>
            <span class="c1"># Use equation B-53 for dead oil in McCain (1990)</span>
            <span class="n">TF</span> <span class="o">=</span> <span class="p">(</span><span class="n">T</span> <span class="o">-</span> <span class="mf">273.15</span><span class="p">)</span> <span class="o">*</span> <span class="mf">9.0</span> <span class="o">/</span> <span class="mf">5.0</span> <span class="o">+</span> <span class="mf">32.0</span>
            <span class="n">mu</span> <span class="o">=</span> <span class="p">(</span><span class="mf">10.</span> <span class="o">**</span> <span class="p">(</span><span class="mf">10.</span> <span class="o">**</span> <span class="p">(</span><span class="mf">1.8653</span> <span class="o">-</span> <span class="mf">0.025086</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">gamma</span> <span class="o">-</span> 
                 <span class="mf">0.5644</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">TF</span><span class="p">)))</span> <span class="o">-</span> <span class="mf">1.0</span><span class="p">)</span> <span class="o">/</span> <span class="mf">1000.</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Particle is solid; thus, viscosity is infinite</span>
            <span class="n">mu</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span>
        
        <span class="k">return</span> <span class="n">mu</span></div>
    
<div class="viewcode-block" id="InsolubleParticle.interface_tension"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.interface_tension.html#dbm.InsolubleParticle.interface_tension">[docs]</a>    <span class="k">def</span> <span class="nf">interface_tension</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">T</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the interfacial tension between the particle and water</span>
<span class="sd">        </span>
<span class="sd">        Computes the interfacial tension between a fluid particle and water.</span>
<span class="sd">        Since for `InsolubleParticle` there is no `composition` we have very</span>
<span class="sd">        little to go on.  This function currently returns the surface </span>
<span class="sd">        tension of seawater.  If solid, the surface tension is returned as</span>
<span class="sd">        infinite.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        sigma_p : float</span>
<span class="sd">            interfacial tension (N/m)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Returns the air-water interfacial tension for lack of any better</span>
<span class="sd">        knowledge about this compound</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isfluid</span><span class="p">:</span>
            <span class="n">S</span> <span class="o">=</span> <span class="mf">34.5</span>
            <span class="n">sigma</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">sigma</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">S</span><span class="p">)</span>
            
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sigma</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span>
        
        <span class="k">return</span> <span class="n">sigma</span></div>
    
<div class="viewcode-block" id="InsolubleParticle.biodegradation_rate"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.biodegradation_rate.html#dbm.InsolubleParticle.biodegradation_rate">[docs]</a>    <span class="k">def</span> <span class="nf">biodegradation_rate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">lag_time</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Determine the biodegradation rate constant</span>
<span class="sd">        </span>
<span class="sd">        Returns the first-order biodegradation rate constant after the </span>
<span class="sd">        simulation time exceeds the bacterial community response lag time.  </span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        t : float</span>
<span class="sd">            current simulation time (s)</span>
<span class="sd">        lag_time : bool, default = True</span>
<span class="sd">            flag indicating whether the biodegradation rates should include</span>
<span class="sd">            a lag time (True) or not (False).  Default value is True.</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        k_bio : ndarray, size (nc)</span>
<span class="sd">            first-order biodegradation rate constant (1/s)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        The duration of the lag time (t_bio) is provided during initialization</span>
<span class="sd">        of an `dbm.InsolubleParticle`.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Determine the correct value of the rate constant</span>
        <span class="n">k_bio</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_bio</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">lag_time</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">t_bio</span> <span class="o">&gt;</span> <span class="n">t</span><span class="p">:</span>
                <span class="n">k_bio</span> <span class="o">=</span> <span class="mf">0.</span>
        
        <span class="k">return</span> <span class="n">k_bio</span></div>


<div class="viewcode-block" id="InsolubleParticle.mass_by_diameter"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.mass_by_diameter.html#dbm.InsolubleParticle.mass_by_diameter">[docs]</a>    <span class="k">def</span> <span class="nf">mass_by_diameter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the mass (kg) of an inert fluid particle with equivalent </span>
<span class="sd">        spherical diameter `de`.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        de : float</span>
<span class="sd">            equivalent spherical diameter (m)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        m : float</span>
<span class="sd">            mass of the fluid particle (kg)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mf">6.0</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">de</span><span class="o">**</span><span class="mi">3</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="InsolubleParticle.diameter"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.diameter.html#dbm.InsolubleParticle.diameter">[docs]</a>    <span class="k">def</span> <span class="nf">diameter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the diameter (m) of an inert fluid particle of mass `m`.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : float</span>
<span class="sd">            mass of the inert fluid particle (m)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        de : diameter of the fluid particle (m)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="mf">6.0</span> <span class="o">*</span> <span class="n">m</span> <span class="o">/</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)))</span><span class="o">**</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="mf">3.0</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="InsolubleParticle.particle_shape"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.particle_shape.html#dbm.InsolubleParticle.particle_shape">[docs]</a>    <span class="k">def</span> <span class="nf">particle_shape</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Determine the shape of an inert fluid particle from the properties of </span>
<span class="sd">        the particle and surrounding fluid.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : float</span>
<span class="sd">            mass of the inert fluid particle (m)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        A tuple containing:</span>
<span class="sd">        </span>
<span class="sd">            shape : integer</span>
<span class="sd">                1 - sphere, 2 - ellipsoid, 3 - spherical cap, 4 - rigid</span>
<span class="sd">            de : float</span>
<span class="sd">                equivalent spherical diameter (m)</span>
<span class="sd">            rho_p : float</span>
<span class="sd">                particle density (kg/m^3)</span>
<span class="sd">            rho : float</span>
<span class="sd">                ambient seawater density (kg/m^3)</span>
<span class="sd">            mu : float</span>
<span class="sd">                ambient seawater dynamic viscosity (Pa s)</span>
<span class="sd">            mu_p : float</span>
<span class="sd">                dispersed phase dynamic viscosity (Pa s)</span>
<span class="sd">            sigma : float</span>
<span class="sd">                interfacial tension (N/m)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        We use the particle temperature to calculate properties at the </span>
<span class="sd">        interface (e.g., to calculate the interfacial tension) and the </span>
<span class="sd">        ambient temperature for properties of the bulk continuous phase </span>
<span class="sd">        (e.g., density and viscosity).</span>
<span class="sd">        </span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_phys.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Compute the fluid particle properties</span>
        <span class="n">de</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">diameter</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span>
        <span class="n">rho_p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span>
        <span class="n">rho</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">mu</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">mu</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">mu_p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">viscosity</span><span class="p">(</span><span class="n">T</span><span class="p">)</span>
        <span class="n">sigma</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">interface_tension</span><span class="p">(</span><span class="n">T</span><span class="p">)</span>
        
        <span class="c1"># Compute the particle shape</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isfluid</span><span class="p">:</span>
            <span class="n">shape</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">particle_shape</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">shape</span> <span class="o">=</span> <span class="mi">4</span>
        
        <span class="k">return</span> <span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">)</span></div>
    
<div class="viewcode-block" id="InsolubleParticle.slip_velocity"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.slip_velocity.html#dbm.InsolubleParticle.slip_velocity">[docs]</a>    <span class="k">def</span> <span class="nf">slip_velocity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">,</span> <span class="n">status</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the slip velocity (m/s) of an inert fluid particle.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : float</span>
<span class="sd">            mass of the inert fluid particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        status : int</span>
<span class="sd">            flag indicating whether the particle is clean (status = 1) or</span>
<span class="sd">            dirty (status = -1).  Default value is -1.</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        us : float</span>
<span class="sd">            slip velocity of the inert particle (m/s)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_phys.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Get the particle properties</span>
        <span class="n">shape</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span> <span class="o">=</span> \
             <span class="bp">self</span><span class="o">.</span><span class="n">particle_shape</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span>
        
        <span class="k">if</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
            <span class="n">us</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">us_sphere</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">us</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">us_ellipsoid</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">us</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">us_spherical_cap</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">)</span>
        
        <span class="k">return</span> <span class="n">us</span></div>
    
<div class="viewcode-block" id="InsolubleParticle.surface_area"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.surface_area.html#dbm.InsolubleParticle.surface_area">[docs]</a>    <span class="k">def</span> <span class="nf">surface_area</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the surface area (m^2) of an inert fluid particle.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : float</span>
<span class="sd">            mass of the inert fluid particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        A : float</span>
<span class="sd">            surface area of the inert particle (m^2)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_phys.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Get the particle properties</span>
        <span class="n">shape</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span> <span class="o">=</span> \
             <span class="bp">self</span><span class="o">.</span><span class="n">particle_shape</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span>
        
        <span class="k">if</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
            <span class="c1"># Compute the surface area of a spherical cap bubble</span>
            <span class="n">us</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">slip_velocity</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span>
            <span class="n">theta_w</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">theta_w_sc</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">)</span>
            <span class="n">A</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">surface_area_sc</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">theta_w</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Compute the area of the equivalent sphere:</span>
            <span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">de</span><span class="o">**</span><span class="mi">2</span>
        
        <span class="k">return</span> <span class="n">A</span></div>
    
<div class="viewcode-block" id="InsolubleParticle.heat_transfer"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.heat_transfer.html#dbm.InsolubleParticle.heat_transfer">[docs]</a>    <span class="k">def</span> <span class="nf">heat_transfer</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">,</span> <span class="n">status</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the heat transfer coefficients (m/s) for an inert fluid </span>
<span class="sd">        particle.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : float</span>
<span class="sd">            mass of the inert fluid particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        status : int</span>
<span class="sd">            flag indicating whether the particle is clean (status = 1) or</span>
<span class="sd">            dirty (status = -1).  Default value is -1.</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        beta_T : float</span>
<span class="sd">            heat transfer coefficient for the inert particle (m/s)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_phys.f95``.</span>
<span class="sd">        </span>
<span class="sd">       &quot;&quot;&quot;</span>
        <span class="c1"># Get the particle properties</span>
        <span class="n">shape</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span> <span class="o">=</span> \
             <span class="bp">self</span><span class="o">.</span><span class="n">particle_shape</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span>
        
        <span class="c1"># Get the thermal conductivity of seawater</span>
        <span class="n">k</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">k</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">seawater</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span> <span class="o">*</span> 
            <span class="n">seawater</span><span class="o">.</span><span class="n">cp</span><span class="p">())</span>
        
        <span class="c1"># Compute the slip velocity</span>
        <span class="n">us</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">slip_velocity</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        
        <span class="c1"># Compute the appropriate heat transfer coefficients.  Assume the </span>
        <span class="c1"># heat transfer has the same form as the mass transfer with the </span>
        <span class="c1"># diffusivity replaced by the thermal conductivity</span>
        <span class="k">if</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_sphere</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> 
                                     <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_ellipsoid</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> 
                                        <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_spherical_cap</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
        
        <span class="k">return</span> <span class="n">beta</span></div>
    
<div class="viewcode-block" id="InsolubleParticle.return_all"><a class="viewcode-back" href="../autodoc/dbm/dbm.InsolubleParticle.return_all.html#dbm.InsolubleParticle.return_all">[docs]</a>    <span class="k">def</span> <span class="nf">return_all</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">,</span> <span class="n">status</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute all of the dynamic properties of an inert fluid particle in </span>
<span class="sd">        an efficient manner (e.g., minimizing replicate calls to functions).</span>
<span class="sd">        </span>
<span class="sd">        This method repeats the calculations in the individual property </span>
<span class="sd">        methods, and does not call the methods already defined.  This is done</span>
<span class="sd">        so that multiple calls to functions (e.g., slip velocity) do not </span>
<span class="sd">        occur.  </span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        m : float</span>
<span class="sd">            mass of the inert fluid particle (kg)</span>
<span class="sd">        T : float</span>
<span class="sd">            particle temperature (K)</span>
<span class="sd">        P : float</span>
<span class="sd">            particle pressure (Pa)</span>
<span class="sd">        Sa : float</span>
<span class="sd">            salinity of ambient seawater (psu)</span>
<span class="sd">        Ta : float</span>
<span class="sd">            temperature of ambient seawater (K)</span>
<span class="sd">        status : int</span>
<span class="sd">            flag indicating whether the particle is clean (status = 1) or</span>
<span class="sd">            dirty (status = -1).  Default value is -1.</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        A tuple containing:</span>
<span class="sd">            shape : integer</span>
<span class="sd">                1 - sphere, 2 - ellipsoid, 3 - spherical cap, 4 - rigid</span>
<span class="sd">            de : float</span>
<span class="sd">                equivalent spherical diameter (m)</span>
<span class="sd">            rho_p : float</span>
<span class="sd">                particle density (kg/m^3)</span>
<span class="sd">            us : float</span>
<span class="sd">                slip velocity (m/s)</span>
<span class="sd">            A : float</span>
<span class="sd">                surface area (m^2)</span>
<span class="sd">            beta_T : float</span>
<span class="sd">                heat transfer coefficient (m/s)</span>
<span class="sd">        </span>
<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Uses the Fortran subroutines in ``./src/dbm_phys.f95``.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Ambient properties of seawater</span>
        <span class="n">rho</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">mu</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">mu</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">sigma</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">interface_tension</span><span class="p">(</span><span class="n">T</span><span class="p">)</span>
        <span class="n">k</span> <span class="o">=</span> <span class="n">seawater</span><span class="o">.</span><span class="n">k</span><span class="p">(</span><span class="n">Ta</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">P</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">rho</span> <span class="o">*</span> <span class="n">seawater</span><span class="o">.</span><span class="n">cp</span><span class="p">())</span>
        
        <span class="c1"># Particle density, equivalent diameter and shape</span>
        <span class="n">rho_p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">density</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">Sa</span><span class="p">,</span> <span class="n">Ta</span><span class="p">)</span>
        <span class="n">de</span> <span class="o">=</span> <span class="p">(</span><span class="mf">6.0</span> <span class="o">*</span> <span class="n">m</span> <span class="o">/</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">rho_p</span><span class="p">))</span><span class="o">**</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="mf">3.0</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isfluid</span><span class="p">:</span>
            <span class="n">shape</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">particle_shape</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">shape</span> <span class="o">=</span> <span class="mi">4</span>
        
        <span class="c1"># Other particle properties</span>
        <span class="n">mu_p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">viscosity</span><span class="p">(</span><span class="n">T</span><span class="p">)</span>
        
        <span class="c1"># Shape-specific properties</span>
        <span class="k">if</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
            <span class="n">us</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">us_sphere</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">)</span>
            <span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">de</span><span class="o">**</span><span class="mi">2</span>
            <span class="n">beta_T</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_sphere</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> 
                                       <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="n">status</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">shape</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">us</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">us_ellipsoid</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">status</span><span class="p">)</span>
            <span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">de</span><span class="o">**</span><span class="mi">2</span>
            <span class="n">beta_T</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_ellipsoid</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">mu_p</span><span class="p">,</span> 
                                          <span class="bp">self</span><span class="o">.</span><span class="n">fp_type</span><span class="p">,</span> <span class="n">status</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">us</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">us_spherical_cap</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">rho</span><span class="p">)</span>
            <span class="n">theta_w</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">theta_w_sc</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">mu</span><span class="p">)</span>
            <span class="n">A</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">surface_area_sc</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">theta_w</span><span class="p">)</span>
            <span class="n">beta_T</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">xfer_spherical_cap</span><span class="p">(</span><span class="n">de</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">rho</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span> 
                                              <span class="n">k</span><span class="p">,</span> <span class="n">status</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        
        <span class="k">return</span> <span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="n">de</span><span class="p">,</span> <span class="n">rho_p</span><span class="p">,</span> <span class="n">us</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="n">beta_T</span><span class="p">)</span></div></div>
    

<span class="c1"># ----------------------------------------------------------------------------</span>
<span class="c1"># Functions used by classes to compute gas/liquid equilibrium of a mixture</span>
<span class="c1"># ----------------------------------------------------------------------------</span>

<div class="viewcode-block" id="equil_MM"><a class="viewcode-back" href="../autodoc/dbm/dbm.equil_MM.html#dbm.equil_MM">[docs]</a><span class="k">def</span> <span class="nf">equil_MM</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> <span class="n">Aij</span><span class="p">,</span> <span class="n">Bij</span><span class="p">,</span> <span class="n">delta_groups</span><span class="p">,</span> 
             <span class="n">calc_delta</span><span class="p">,</span> <span class="n">K_0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute the equilibrium composition of a mixture using the P-R EOS</span>
<span class="sd">    </span>
<span class="sd">    Computes the mole fraction composition for the gas and liquid phases of a</span>
<span class="sd">    mixture using the Peng-Robinson equation of state and the methodology</span>
<span class="sd">    described Michelsen and Mollerup (2007).  For multiphase equilibria, </span>
<span class="sd">    the successive substition method is used.  If several iterations suggest</span>
<span class="sd">    a single-phase equilibrium, stability analysis is used to verify the</span>
<span class="sd">    prediction.  If a two-phase result is predicted by stability analysis, </span>
<span class="sd">    successive substitution continues with an improved estimate for the </span>
<span class="sd">    composition; otherwise, the single phase result is returned.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    m : ndarray, size (nc)</span>
<span class="sd">        masses of each component present in the whole mixture (gas plus </span>
<span class="sd">        liquid, kg)</span>
<span class="sd">    T : float</span>
<span class="sd">        temperature (K)</span>
<span class="sd">    P : float</span>
<span class="sd">        pressure (Pa)</span>
<span class="sd">    M : ndarray, size (nc)</span>
<span class="sd">        Molecular weights (kg/mol)</span>
<span class="sd">    Pc : ndarray, size (nc)</span>
<span class="sd">        Critical pressures (Pa)</span>
<span class="sd">    Tc : ndarray, size (nc)</span>
<span class="sd">        Critical temperatures (K)</span>
<span class="sd">    omega : ndarray, size (nc)</span>
<span class="sd">        Acentric factors (--)</span>
<span class="sd">    delta : ndarray, size (nc, nc)</span>
<span class="sd">        Binary interaction coefficients for the Peng-Robinson equation of </span>
<span class="sd">        state.  </span>
<span class="sd">    Aij : ndarray, (15, 15)</span>
<span class="sd">        Coefficients in matrix A_ij for the group contribution method for </span>
<span class="sd">        delta_ij following Privat and Jaubert (2012)</span>
<span class="sd">    Bij : ndarray, (15, 15)</span>
<span class="sd">        Coefficients in matrix A_ij for the group contribution method for </span>
<span class="sd">        delta_ij following Privat and Jaubert (2012)</span>
<span class="sd">    delta_groups : ndarray, (nc, 15)</span>
<span class="sd">        Specification of the fractional groups for each component of the </span>
<span class="sd">        mixture for the group contribution method of Privat and Jaubert (2012)</span>
<span class="sd">        for delta_ij</span>
<span class="sd">    calc_delta : int</span>
<span class="sd">        Flag specifying whether or not to compute delta_ij (1: True, -1: </span>
<span class="sd">        False) using the group contribution method</span>
<span class="sd">    K_0 : ndarray, size (nc)</span>
<span class="sd">        Initial guess for the partition coefficients.  If K = None, this </span>
<span class="sd">        function will use initial estimates from Wilson (see Michelsen and</span>
<span class="sd">        Mollerup, 2007, page 259, equation 26)</span>
<span class="sd">    </span>
<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    xi : ndarray, size(2, nc)</span>
<span class="sd">        Mole fraction of each component in the mixture.  Row 1 gives the</span>
<span class="sd">        values for the gas phase and Row 2 gives the values for the liquid </span>
<span class="sd">        phase (--)</span>
<span class="sd">    </span>
<span class="sd">    Notes</span>
<span class="sd">    -----</span>
<span class="sd">    The method estimates the K-factors giving the mole fractions in gas and</span>
<span class="sd">    liquid and optimizes the K-factor estimates until they converge on the</span>
<span class="sd">    ratio of the actual fugacity coefficients (phi_liq / phi_gas).</span>
<span class="sd">    Convergence uses a squared relative error as in McCain (1990).</span>
<span class="sd">    </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Compute the some constant properties of the mixture</span>
    <span class="n">moles</span> <span class="o">=</span> <span class="n">m</span> <span class="o">/</span> <span class="n">M</span>
    <span class="n">zi</span> <span class="o">=</span> <span class="n">moles</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">moles</span><span class="p">)</span>
    <span class="n">f_zi</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">fugacity</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">zi</span><span class="o">*</span><span class="n">M</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> 
                          <span class="n">Aij</span><span class="p">,</span> <span class="n">Bij</span><span class="p">,</span> <span class="n">delta_groups</span><span class="p">,</span> <span class="n">calc_delta</span><span class="p">)[</span><span class="mi">0</span><span class="p">,:]</span>
    <span class="n">phi_zi</span> <span class="o">=</span> <span class="n">f_zi</span> <span class="o">/</span> <span class="p">(</span><span class="n">zi</span> <span class="o">*</span> <span class="n">P</span><span class="p">)</span>
    <span class="n">di</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">zi</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">phi_zi</span><span class="p">)</span>
    
    <span class="c1"># Compute the total Gibbs energy</span>
    <span class="k">def</span> <span class="nf">gibbs_energy</span><span class="p">(</span><span class="n">K</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the Gibbs energy difference between the feed and the current</span>
<span class="sd">        composition given by K using equation (41) on page 266</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Use the current K to compute the equilibrium</span>
        <span class="n">xi</span><span class="p">,</span> <span class="n">beta</span> <span class="o">=</span> <span class="n">gas_liq_eq</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">K</span><span class="p">)</span>
        
        <span class="c1"># Compute the fugacities of the new composition</span>
        <span class="n">f_gas</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">fugacity</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">xi</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span><span class="o">*</span><span class="n">M</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> 
                               <span class="n">Aij</span><span class="p">,</span> <span class="n">Bij</span><span class="p">,</span> <span class="n">delta_groups</span><span class="p">,</span> <span class="n">calc_delta</span><span class="p">)[</span><span class="mi">0</span><span class="p">,:]</span>
        <span class="n">f_liq</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">fugacity</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">xi</span><span class="p">[</span><span class="mi">1</span><span class="p">,:]</span><span class="o">*</span><span class="n">M</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> 
                               <span class="n">Aij</span><span class="p">,</span> <span class="n">Bij</span><span class="p">,</span> <span class="n">delta_groups</span><span class="p">,</span> <span class="n">calc_delta</span><span class="p">)[</span><span class="mi">1</span><span class="p">,:]</span>
        
        <span class="c1"># Get the fugacity coefficients</span>
        <span class="n">phi_gas</span> <span class="o">=</span> <span class="n">f_gas</span> <span class="o">/</span> <span class="p">(</span><span class="n">xi</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span> <span class="o">*</span> <span class="n">P</span><span class="p">)</span>
        <span class="n">phi_liq</span> <span class="o">=</span> <span class="n">f_liq</span> <span class="o">/</span> <span class="p">(</span><span class="n">xi</span><span class="p">[</span><span class="mi">1</span><span class="p">,:]</span> <span class="o">*</span> <span class="n">P</span><span class="p">)</span>
        
        <span class="c1"># Compute the reduced tangent plane distances</span>
        <span class="n">tpdx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">xi</span><span class="p">[</span><span class="mi">1</span><span class="p">,:]</span> <span class="o">*</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">xi</span><span class="p">[</span><span class="mi">1</span><span class="p">,:])</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">phi_liq</span><span class="p">)</span> <span class="o">-</span> <span class="n">di</span><span class="p">))</span>
        <span class="n">tpdy</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">(</span><span class="n">xi</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span> <span class="o">*</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">xi</span><span class="p">[</span><span class="mi">0</span><span class="p">,:])</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">phi_gas</span><span class="p">)</span> <span class="o">-</span> <span class="n">di</span><span class="p">))</span>
        
        <span class="c1"># Compute the change in the total Gibbs energy between the feed </span>
        <span class="c1"># and this present composition</span>
        <span class="n">DG_RT</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">-</span> <span class="n">beta</span><span class="p">)</span> <span class="o">*</span> <span class="n">tpdx</span> <span class="o">+</span> <span class="n">beta</span> <span class="o">*</span> <span class="n">tpdy</span>
        
        <span class="c1"># Return the results</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">DG_RT</span><span class="p">,</span> <span class="n">tpdx</span><span class="p">,</span> <span class="n">tpdy</span><span class="p">,</span> <span class="n">phi_liq</span><span class="p">,</span> <span class="n">phi_gas</span><span class="p">)</span>
    
    <span class="c1"># Get an initial estimate for the K-factors </span>
    <span class="k">if</span> <span class="n">K_0</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="c1"># Use equation (26) on page 259 of Michelson and Mollerup (2007)</span>
        <span class="n">K</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mf">5.37</span> <span class="o">*</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">+</span> <span class="n">omega</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">Tc</span> <span class="o">/</span> <span class="n">T</span><span class="p">))</span> <span class="o">/</span> <span class="p">(</span><span class="n">P</span> <span class="o">/</span> <span class="n">Pc</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">K</span> <span class="o">=</span> <span class="n">K_0</span>
    
    <span class="c1"># Follow the procedure on page 266ff of Michelsen and Mollerup (2007).    </span>
    <span class="c1"># Start with three iterations of successive substitution</span>
    <span class="n">K</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">xi</span><span class="p">,</span> <span class="n">exit_flag</span><span class="p">,</span> <span class="n">steps</span> <span class="o">=</span> <span class="n">successive_substitution</span><span class="p">(</span>
                                 <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> <span class="n">Aij</span><span class="p">,</span> 
                                 <span class="n">Bij</span><span class="p">,</span> <span class="n">delta_groups</span><span class="p">,</span> <span class="n">calc_delta</span><span class="p">,</span> <span class="n">K</span><span class="p">)</span>
    
    <span class="c1"># Continue iterating if necessary until the solution converges</span>
    <span class="k">while</span> <span class="n">exit_flag</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
        <span class="c1"># Test the total Gibbs energy to decide how to proceed.</span>
        <span class="n">Delta_G_RT</span><span class="p">,</span> <span class="n">tpdx</span><span class="p">,</span> <span class="n">tpdy</span><span class="p">,</span> <span class="n">phi_liq</span><span class="p">,</span> <span class="n">phi_gas</span> <span class="o">=</span> <span class="n">gibbs_energy</span><span class="p">(</span><span class="n">K</span><span class="p">)</span>
        
        <span class="k">if</span> <span class="n">exit_flag</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            
            <span class="k">if</span> <span class="n">Delta_G_RT</span> <span class="o">&lt;</span> <span class="mf">0.</span><span class="p">:</span>
                <span class="c1"># The current composition is converging on a lower total Gibbs</span>
                <span class="c1"># energy than the feed: continue successive substitution</span>
                <span class="n">phases</span> <span class="o">=</span> <span class="mi">2</span>
                
            <span class="k">elif</span> <span class="n">tpdy</span> <span class="o">&lt;</span> <span class="mf">0.</span><span class="p">:</span>
                <span class="c1"># The feed is unstable, but we need a better estimate of K</span>
                <span class="n">K</span> <span class="o">=</span> <span class="n">phi_zi</span> <span class="o">/</span> <span class="n">phi_gas</span>
                <span class="n">phases</span> <span class="o">=</span> <span class="mi">2</span>
                
            <span class="k">elif</span> <span class="n">tpdx</span> <span class="o">&lt;</span> <span class="mf">0.</span><span class="p">:</span>
                <span class="c1"># The feed is unstable, but we need a better estimate of K</span>
                <span class="n">K</span> <span class="o">=</span> <span class="n">phi_liq</span> <span class="o">/</span> <span class="n">phi_zi</span>
                <span class="n">phases</span> <span class="o">=</span> <span class="mi">2</span>
                
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># We are not sure of the stability of the feed:  do stability</span>
                <span class="c1"># analysis.</span>
                <span class="n">K</span><span class="p">,</span> <span class="n">phases</span> <span class="o">=</span> <span class="n">stability_analysis</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> 
                                               <span class="n">delta</span><span class="p">,</span> <span class="n">Aij</span><span class="p">,</span> <span class="n">Bij</span><span class="p">,</span> <span class="n">delta_groups</span><span class="p">,</span>
                                               <span class="n">calc_delta</span><span class="p">,</span> <span class="n">K</span><span class="p">,</span> <span class="n">zi</span><span class="p">,</span> <span class="n">di</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Successive substitution thinks this is single-phase...check</span>
            <span class="c1"># with stability analysis</span>
            <span class="n">K_st</span><span class="p">,</span> <span class="n">phases</span> <span class="o">=</span> <span class="n">stability_analysis</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> 
                                           <span class="n">delta</span><span class="p">,</span> <span class="n">Aij</span><span class="p">,</span> <span class="n">Bij</span><span class="p">,</span> <span class="n">delta_groups</span><span class="p">,</span>
                                           <span class="n">calc_delta</span><span class="p">,</span> <span class="n">K</span><span class="p">,</span> <span class="n">zi</span><span class="p">,</span> <span class="n">di</span><span class="p">)</span>
            
            <span class="c1"># Keep the K-values with the lowest Gibbs energy</span>
            <span class="n">Delta_G_RT_st</span><span class="p">,</span> <span class="n">tpdx_st</span><span class="p">,</span> <span class="n">tpdy_st</span><span class="p">,</span> <span class="n">phi_liq_st</span><span class="p">,</span> <span class="n">phi_gas_st</span> <span class="o">=</span> \
                <span class="n">gibbs_energy</span><span class="p">(</span><span class="n">K</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">Delta_G_RT_st</span> <span class="o">&lt;</span> <span class="n">Delta_G_RT</span><span class="p">:</span>
                <span class="n">K</span> <span class="o">=</span> <span class="n">K_st</span>
            
        <span class="k">if</span> <span class="n">phases</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="c1"># The mixture is unstable and unconverged, continue with</span>
            <span class="c1"># successive substitution</span>
            <span class="n">K</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">xi</span><span class="p">,</span> <span class="n">exit_flag</span><span class="p">,</span> <span class="n">steps</span> <span class="o">=</span> <span class="n">successive_substitution</span><span class="p">(</span>
                                         <span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> 
                                         <span class="n">delta</span><span class="p">,</span> <span class="n">Aij</span><span class="p">,</span> <span class="n">Bij</span><span class="p">,</span>  <span class="n">delta_groups</span><span class="p">,</span> 
                                         <span class="n">calc_delta</span><span class="p">,</span> <span class="n">K</span><span class="p">,</span> <span class="n">steps</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># The mixture is single-phase and converged</span>
            <span class="n">exit_flag</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="n">xi</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">zi</span><span class="p">)))</span>
            <span class="k">if</span> <span class="n">beta</span> <span class="o">&gt;</span> <span class="mf">0.5</span><span class="p">:</span>
                <span class="c1"># Pure gas</span>
                <span class="n">beta</span> <span class="o">=</span> <span class="mf">1.</span>
                <span class="n">xi</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span> <span class="o">=</span> <span class="n">zi</span>
                <span class="n">K</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">K</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Pure liquid</span>
                <span class="n">beta</span> <span class="o">=</span> <span class="mf">0.</span>
                <span class="n">xi</span><span class="p">[</span><span class="mi">1</span><span class="p">,:]</span> <span class="o">=</span> <span class="n">zi</span>
                <span class="n">K</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">K</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
    
    <span class="c1"># Return the optimized mixture composition</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">xi</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">K</span><span class="p">)</span></div>


<div class="viewcode-block" id="stability_analysis"><a class="viewcode-back" href="../autodoc/dbm/dbm.stability_analysis.html#dbm.stability_analysis">[docs]</a><span class="k">def</span> <span class="nf">stability_analysis</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> <span class="n">Aij</span><span class="p">,</span> <span class="n">Bij</span><span class="p">,</span> 
                       <span class="n">delta_groups</span><span class="p">,</span> <span class="n">calc_delta</span><span class="p">,</span> <span class="n">K</span><span class="p">,</span> <span class="n">zi</span><span class="p">,</span> <span class="n">di</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Perform stability analysis to determine the stability of a mixture</span>
<span class="sd">    </span>
<span class="sd">    Perform the stabilty analysis steps in Michelsen and Mollerup (2007) to</span>
<span class="sd">    determine the stability of a mixture</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    m : ndarray, size (nc)</span>
<span class="sd">        masses of each component present in the whole mixture (gas plus </span>
<span class="sd">        liquid, kg)</span>
<span class="sd">    T : float</span>
<span class="sd">        temperature (K)</span>
<span class="sd">    P : float</span>
<span class="sd">        pressure (Pa)</span>
<span class="sd">    M : ndarray, size (nc)</span>
<span class="sd">        Molecular weights (kg/mol)</span>
<span class="sd">    Pc : ndarray, size (nc)</span>
<span class="sd">        Critical pressures (Pa)</span>
<span class="sd">    Tc : ndarray, size (nc)</span>
<span class="sd">        Critical temperatures (K)</span>
<span class="sd">    omega : ndarray, size (nc)</span>
<span class="sd">        Acentric factors (--)</span>
<span class="sd">    delta : ndarray, size (nc, nc)</span>
<span class="sd">        Binary interaction coefficients for the Peng-Robinson equation of </span>
<span class="sd">        state.  </span>
<span class="sd">    Aij : ndarray, (15, 15)</span>
<span class="sd">        Coefficients in matrix A_ij for the group contribution method for </span>
<span class="sd">        delta_ij following Privat and Jaubert (2012)</span>
<span class="sd">    Bij : ndarray, (15, 15)</span>
<span class="sd">        Coefficients in matrix A_ij for the group contribution method for </span>
<span class="sd">        delta_ij following Privat and Jaubert (2012)</span>
<span class="sd">    delta_groups : ndarray, (nc, 15)</span>
<span class="sd">        Specification of the fractional groups for each component of the </span>
<span class="sd">        mixture for the group contribution method of Privat and Jaubert (2012)</span>
<span class="sd">        for delta_ij</span>
<span class="sd">    calc_delta : int</span>
<span class="sd">        Flag specifying whether or not to compute delta_ij (1: True, -1: </span>
<span class="sd">        False) using the group contribution method</span>
<span class="sd">    K : ndarray, size (nc)</span>
<span class="sd">        Initial guess for the partition coefficients.  If K = None, this </span>
<span class="sd">        function will use initial estimates from Wilson (see Michelsen and</span>
<span class="sd">        Mollerup, 2007, page 259, equation 26)</span>
<span class="sd">    di : ndarray, size (nc)</span>
<span class="sd">        Mixture property ln(zi) + ln(phi(zi)); see Michelsen and Mollerup</span>
<span class="sd">        (2007) page 267</span>
<span class="sd">    </span>
<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    K : ndarray, size (nc)</span>
<span class="sd">        Updated estimate for the K factors after stability analysis</span>
<span class="sd">    phases : int</span>
<span class="sd">        Number of phases in the mixture (2 or 1)</span>
<span class="sd">    </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Compute the mole fraction of the total mixture (called the feed in </span>
    <span class="c1"># Michelsen and Mollerup, 2007)</span>
    <span class="n">moles</span> <span class="o">=</span> <span class="n">m</span> <span class="o">/</span> <span class="n">M</span>
    <span class="n">zi</span> <span class="o">=</span> <span class="n">moles</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">moles</span><span class="p">)</span>
    
    <span class="c1"># Generate the update equation for finding W that minizes tm</span>
    <span class="k">def</span> <span class="nf">update_W</span><span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="n">phase</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Update the estimate for W to minimize the modified tangent plane</span>
<span class="sd">        distance using equation (51) on page 269 in Michelsen and Mollerup</span>
<span class="sd">        (2007).</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        W : ndarray</span>
<span class="sd">            Current estimate for the composition (moles)</span>
<span class="sd">        phase : int</span>
<span class="sd">            Assumed phase of the current composition (0: gas, 1: liquid)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        W : ndarray</span>
<span class="sd">            New estimate of W (moles)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Compute the fugacity at the composition W</span>
        <span class="n">f_W</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">fugacity</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">W</span><span class="o">*</span><span class="n">M</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> 
                             <span class="n">Aij</span><span class="p">,</span> <span class="n">Bij</span><span class="p">,</span> <span class="n">delta_groups</span><span class="p">,</span> <span class="n">calc_delta</span><span class="p">)[</span><span class="n">phase</span><span class="p">,:]</span>
        
        <span class="c1"># Get the fugacity coefficients</span>
        <span class="n">phi_W</span> <span class="o">=</span> <span class="n">f_W</span> <span class="o">/</span> <span class="p">(</span><span class="n">W</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">W</span><span class="p">)</span> <span class="o">*</span> <span class="n">P</span><span class="p">)</span>
        
        <span class="c1"># Return a new estimate of W</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">di</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">phi_W</span><span class="p">))</span>
    
    <span class="c1"># Compute the modified tangent plane distance</span>
    <span class="k">def</span> <span class="nf">compute_tm</span><span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="n">phase</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the modified tangent plane distance according to equation (44)</span>
<span class="sd">        in Michelsen and Mollerup (2007) on page 267.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        W : ndarray</span>
<span class="sd">            Current estimate for the composition (moles)</span>
<span class="sd">        phase : int</span>
<span class="sd">            Assumed phase of the current composition (0: gas, 1: liquid)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        tm : float</span>
<span class="sd">            Value of the modified tangent plane distance for the given </span>
<span class="sd">            composition</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Compute the fugacity at the composition W</span>
        <span class="n">f_W</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">fugacity</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">W</span><span class="o">*</span><span class="n">M</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> 
                             <span class="n">Aij</span><span class="p">,</span> <span class="n">Bij</span><span class="p">,</span> <span class="n">delta_groups</span><span class="p">,</span> <span class="n">calc_delta</span><span class="p">)[</span><span class="n">phase</span><span class="p">,:]</span>
        
        <span class="c1"># Get the fugacity coefficients</span>
        <span class="n">phi_W</span> <span class="o">=</span> <span class="n">f_W</span> <span class="o">/</span> <span class="p">(</span><span class="n">W</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">W</span><span class="p">)</span> <span class="o">*</span> <span class="n">P</span><span class="p">)</span>
        
        <span class="c1"># Return the modified tangent plane distance, equation (44) on page</span>
        <span class="c1"># 267</span>
        <span class="k">return</span> <span class="mf">1.</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">W</span> <span class="o">*</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">W</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">phi_W</span><span class="p">)</span> <span class="o">-</span> <span class="n">di</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">))</span>
    
    <span class="c1"># Solve for W that minimizes tm</span>
    <span class="k">def</span> <span class="nf">find_W</span><span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="n">phase</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Use successive subsitution to find a value of W that minimizes the </span>
<span class="sd">        modified tangent plane distance and then interpret the stability </span>
<span class="sd">        of the mixture based on the results</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        W : ndarray</span>
<span class="sd">            Current estimate for the composition (moles)</span>
<span class="sd">        phase : int</span>
<span class="sd">            Assumed phase of the current composition (0: gas, 1: liquid)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        W : ndarray</span>
<span class="sd">            Final value of the composition (moles)</span>
<span class="sd">        tm : float</span>
<span class="sd">            Value of the modified tangent plane distance for the final</span>
<span class="sd">            composition</span>
<span class="sd">        phases : int</span>
<span class="sd">            Evaluation of the number of phases present (1 or 2)</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Set up the iteration parameters</span>
        <span class="n">tol</span> <span class="o">=</span> <span class="mf">1.49012e-8</span>  <span class="c1"># Use same value as for K-factor iteration</span>
        <span class="n">err</span> <span class="o">=</span> <span class="mf">1.</span>
        
        <span class="c1"># Iterate to find the final value of W</span>
        <span class="k">while</span> <span class="n">err</span> <span class="o">&gt;</span> <span class="n">tol</span><span class="p">:</span>
            <span class="c1"># Save the current value of W</span>
            <span class="n">W_old</span> <span class="o">=</span> <span class="n">W</span>
            
            <span class="c1"># Update the estimate of W using the update equation</span>
            <span class="n">W</span> <span class="o">=</span> <span class="n">update_W</span><span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="n">phase</span><span class="p">)</span>
            
            <span class="c1"># Compute the current error based on the squared relative error </span>
            <span class="c1"># suggested by McCain (1990)</span>
            <span class="n">err</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">((</span><span class="n">W</span> <span class="o">-</span> <span class="n">W_old</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">/</span> <span class="p">(</span><span class="n">W</span> <span class="o">*</span> <span class="n">W_old</span><span class="p">))</span>
        
        <span class="c1"># Compute the modified tangent plane distance</span>
        <span class="n">tm</span> <span class="o">=</span> <span class="n">compute_tm</span><span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="n">phase</span><span class="p">)</span>
        
        <span class="c1"># Determine if we found a trivial solution</span>
        <span class="n">trivial</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">W</span><span class="p">)):</span>
            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">W</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">zi</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">&gt;</span> <span class="mf">1.e-5</span><span class="p">:</span>
                <span class="n">trivial</span> <span class="o">=</span> <span class="kc">False</span>
        
        <span class="c1"># Evaluate the stability of the outcome</span>
        <span class="k">if</span> <span class="n">tm</span> <span class="o">&lt;</span> <span class="mf">0.</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">trivial</span><span class="p">:</span>
            <span class="n">phases</span> <span class="o">=</span> <span class="mi">2</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># This is a single-phase gas</span>
            <span class="n">phases</span> <span class="o">=</span> <span class="mi">1</span>
        
        <span class="c1"># Return the results</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="n">tm</span><span class="p">,</span> <span class="n">phases</span><span class="p">)</span>
    
    <span class="c1"># First, do a test vapor-like composition</span>
    <span class="n">W</span> <span class="o">=</span> <span class="n">K</span> <span class="o">*</span> <span class="n">zi</span>
    <span class="n">W_gas</span><span class="p">,</span> <span class="n">tm_gas</span><span class="p">,</span> <span class="n">phases_gas</span> <span class="o">=</span> <span class="n">find_W</span><span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="n">K_gas</span> <span class="o">=</span> <span class="n">W_gas</span> <span class="o">/</span> <span class="n">zi</span>
    
    <span class="c1"># Second, to be conservative, do a test liquid-like composition</span>
    <span class="n">W</span> <span class="o">=</span> <span class="n">zi</span> <span class="o">/</span> <span class="n">K</span>
    <span class="n">W_liq</span><span class="p">,</span> <span class="n">tm_liq</span><span class="p">,</span> <span class="n">phases_liq</span> <span class="o">=</span> <span class="n">find_W</span><span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">K_liq</span> <span class="o">=</span> <span class="n">zi</span> <span class="o">/</span> <span class="n">W_liq</span>
    
    <span class="k">if</span> <span class="n">phases_gas</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">phases_liq</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">tm_gas</span> <span class="o">&lt;</span> <span class="n">tm_liq</span><span class="p">:</span>
            <span class="c1"># This is probably a gas-like mixture</span>
            <span class="n">K</span> <span class="o">=</span> <span class="n">K_gas</span>
            <span class="n">phases</span> <span class="o">=</span> <span class="mi">2</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># This is probably a liquid-like mixture</span>
            <span class="n">K</span> <span class="o">=</span> <span class="n">K_liq</span>
            <span class="n">phases</span> <span class="o">=</span> <span class="mi">2</span>
    <span class="k">elif</span> <span class="n">phases_gas</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="c1"># This is proably a gas-like mixture</span>
        <span class="n">K</span> <span class="o">=</span> <span class="n">K_gas</span>
        <span class="n">phases</span> <span class="o">=</span> <span class="mi">2</span>
    <span class="k">elif</span> <span class="n">phases_liq</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="c1"># This is probably a liquid-like mixture</span>
        <span class="n">K</span> <span class="o">=</span> <span class="n">K_liq</span>
        <span class="n">phases</span> <span class="o">=</span> <span class="mi">2</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># This is a single-phase mixture</span>
        <span class="n">K</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">K</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
        <span class="n">phases</span> <span class="o">=</span> <span class="mi">1</span>
    
    <span class="c1"># Return the results</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">K</span><span class="p">,</span> <span class="n">phases</span><span class="p">)</span></div>


<div class="viewcode-block" id="successive_substitution"><a class="viewcode-back" href="../autodoc/dbm/dbm.successive_substitution.html#dbm.successive_substitution">[docs]</a><span class="k">def</span> <span class="nf">successive_substitution</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">max_iter</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> <span class="n">Aij</span><span class="p">,</span> 
                            <span class="n">Bij</span><span class="p">,</span> <span class="n">delta_groups</span><span class="p">,</span> <span class="n">calc_delta</span><span class="p">,</span> <span class="n">K</span><span class="p">,</span> <span class="n">steps</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Find K-factors by successive substitution</span>
<span class="sd">    </span>
<span class="sd">    Iterate to find a converged set of K-factors defining the gas/liquid</span>
<span class="sd">    partitioning of a mixture using successive substitution.  We follow the</span>
<span class="sd">    algorithms in McCain (1990) and Michelsen and Mollerup (2007).</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    m : ndarray, size (nc)</span>
<span class="sd">        masses of each component present in the whole mixture (gas plus </span>
<span class="sd">        liquid, kg)</span>
<span class="sd">    T : float</span>
<span class="sd">        temperature (K)</span>
<span class="sd">    P : float</span>
<span class="sd">        pressure (Pa)</span>
<span class="sd">    max_iter : int</span>
<span class="sd">        maximum number of iterations to perform.  Set max_iter to np.inf if</span>
<span class="sd">        you want the algorithm to guarantee to iterate to convergenece, but </span>
<span class="sd">        beware that you may create an infinite loop.</span>
<span class="sd">    M : ndarray, size (nc)</span>
<span class="sd">        Molecular weights (kg/mol)</span>
<span class="sd">    Pc : ndarray, size (nc)</span>
<span class="sd">        Critical pressures (Pa)</span>
<span class="sd">    Tc : ndarray, size (nc)</span>
<span class="sd">        Critical temperatures (K)</span>
<span class="sd">    omega : ndarray, size (nc)</span>
<span class="sd">        Acentric factors (--)</span>
<span class="sd">    delta : ndarray, size (nc, nc)</span>
<span class="sd">        Binary interaction coefficients for the Peng-Robinson equation of </span>
<span class="sd">        state.  </span>
<span class="sd">    Aij : ndarray, (15, 15)</span>
<span class="sd">        Coefficients in matrix A_ij for the group contribution method for </span>
<span class="sd">        delta_ij following Privat and Jaubert (2012)</span>
<span class="sd">    Bij : ndarray, (15, 15)</span>
<span class="sd">        Coefficients in matrix A_ij for the group contribution method for </span>
<span class="sd">        delta_ij following Privat and Jaubert (2012)</span>
<span class="sd">    delta_groups : ndarray, (nc, 15)</span>
<span class="sd">        Specification of the fractional groups for each component of the </span>
<span class="sd">        mixture for the group contribution method of Privat and Jaubert (2012)</span>
<span class="sd">        for delta_ij</span>
<span class="sd">    calc_delta : int</span>
<span class="sd">        Flag specifying whether or not to compute delta_ij (1: True, -1: </span>
<span class="sd">        False) using the group contribution method</span>
<span class="sd">    K : ndarray, size (nc)</span>
<span class="sd">        Initial guess for the partition coefficients.  If K = None, this </span>
<span class="sd">        function will use initial estimates from Wilson (see Michelsen and</span>
<span class="sd">        Mollerup, 2007, page 259, equation 26)</span>
<span class="sd">    steps : int (default = 0)</span>
<span class="sd">        Number of previous iteration steps</span>
<span class="sd">    </span>
<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    K : ndarray, size (nc)</span>
<span class="sd">        Final value of the K-factors</span>
<span class="sd">    beta : float</span>
<span class="sd">        Fraction of gas or liquid (--)</span>
<span class="sd">    xi : ndarray, size(2, nc)</span>
<span class="sd">        Mole fraction of each component in the mixture.  Row 1 gives the</span>
<span class="sd">        values for the gas phase and Row 2 gives the values for the liquid </span>
<span class="sd">        phase (--)</span>
<span class="sd">    exit_flag : int</span>
<span class="sd">        Flag indicating how the solution finished:  1: converged in the </span>
<span class="sd">        allowable number of iterations, 0: did not converge and did not find</span>
<span class="sd">        any indication that it might be single phase, and -1: did not </span>
<span class="sd">        converge, but it looks like it might be single phase.</span>
<span class="sd">    steps : int</span>
<span class="sd">        Total number of interation steps so far</span>
<span class="sd">    </span>
<span class="sd">    Notes</span>
<span class="sd">    -----</span>
<span class="sd">    The max_iter parameter controls how many steps of successive iteration </span>
<span class="sd">    are performed.  If set to None, the iteration will continue until the </span>
<span class="sd">    tolerance criteria are reached.</span>
<span class="sd">    </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Update the value of K using successive substitution</span>
    <span class="k">def</span> <span class="nf">update_K</span><span class="p">(</span><span class="n">K</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Evaluate the update function for finding K-factor</span>
<span class="sd">        </span>
<span class="sd">        Evaluates the new guess for K-factor following McCain (1990) p. 426, </span>
<span class="sd">        equation (15-23) as explained on p. 430 in connection with equation</span>
<span class="sd">        (15-31).  This is the update equation for the successive substitution </span>
<span class="sd">        method.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        T, P, m_0, M, Pc, Tc, omega, delta = constant and inherited</span>
<span class="sd">            from above</span>
<span class="sd">        K : ndarray</span>
<span class="sd">            The current guess for the K-factor (--)</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        K_new : ndarray</span>
<span class="sd">            New guess for K-factor</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        
        <span class="c1"># Get the mixture composition for the current K-factor</span>
        <span class="n">xi</span><span class="p">,</span> <span class="n">beta</span> <span class="o">=</span> <span class="n">gas_liq_eq</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">K</span><span class="p">)</span>
        
        <span class="c1"># Get tha gas and liquid fugacities for the current composition</span>
        <span class="n">f_gas</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">fugacity</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">xi</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span><span class="o">*</span><span class="n">M</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> 
                               <span class="n">Aij</span><span class="p">,</span> <span class="n">Bij</span><span class="p">,</span> <span class="n">delta_groups</span><span class="p">,</span> <span class="n">calc_delta</span><span class="p">)[</span><span class="mi">0</span><span class="p">,:]</span>
        <span class="n">f_liq</span> <span class="o">=</span> <span class="n">dbm_f</span><span class="o">.</span><span class="n">fugacity</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">P</span><span class="p">,</span> <span class="n">xi</span><span class="p">[</span><span class="mi">1</span><span class="p">,:]</span><span class="o">*</span><span class="n">M</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">Pc</span><span class="p">,</span> <span class="n">Tc</span><span class="p">,</span> <span class="n">omega</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> 
                               <span class="n">Aij</span><span class="p">,</span> <span class="n">Bij</span><span class="p">,</span> <span class="n">delta_groups</span><span class="p">,</span> <span class="n">calc_delta</span><span class="p">)[</span><span class="mi">1</span><span class="p">,:]</span>
        
        <span class="c1"># Update K using K = (phi_liq / phi_gas)</span>
        <span class="n">K_new</span> <span class="o">=</span> <span class="p">(</span><span class="n">f_liq</span> <span class="o">/</span> <span class="p">(</span><span class="n">xi</span><span class="p">[</span><span class="mi">1</span><span class="p">,:]</span> <span class="o">*</span> <span class="n">P</span><span class="p">))</span> <span class="o">/</span> <span class="p">(</span><span class="n">f_gas</span> <span class="o">/</span> <span class="p">(</span><span class="n">xi</span><span class="p">[</span><span class="mi">0</span><span class="p">,:]</span> <span class="o">*</span> <span class="n">P</span><span class="p">))</span>
        
        <span class="c1"># If the mass of any component in the mixture is zero, make sure the</span>
        <span class="c1"># K-factor is also zero.</span>
        <span class="n">K_new</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">K_new</span><span class="p">)]</span> <span class="o">=</span> <span class="mf">0.</span>
        
        <span class="c1"># Return an updated value for the K factors</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">K_new</span><span class="p">,</span> <span class="n">beta</span><span class="p">)</span>
    
    <span class="c1"># Set up the iteration parameters</span>
    <span class="n">tol</span> <span class="o">=</span> <span class="mf">1.49012e-8</span>  <span class="c1"># Suggested by McCain (1990)</span>
    <span class="n">err</span> <span class="o">=</span> <span class="mf">1.</span>
    
    <span class="c1"># Iterate to find the final value of K factor using successive </span>
    <span class="c1"># substitution</span>
    <span class="n">stop</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="k">while</span> <span class="n">err</span> <span class="o">&gt;</span> <span class="n">tol</span> <span class="ow">and</span> <span class="n">steps</span> <span class="o">&lt;</span> <span class="n">max_iter</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">stop</span><span class="p">:</span>
        <span class="c1"># Save the current value of K factor</span>
        <span class="n">K_old</span> <span class="o">=</span> <span class="n">K</span>
        
        <span class="c1"># Update the estimate of K factor using the present fugacities</span>
        <span class="n">K</span><span class="p">,</span> <span class="n">beta</span> <span class="o">=</span> <span class="n">update_K</span><span class="p">(</span><span class="n">K</span><span class="p">)</span>
        <span class="n">steps</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="n">steps</span> <span class="o">&gt;</span> <span class="mi">3</span> <span class="ow">and</span> <span class="p">(</span><span class="n">beta</span> <span class="o">==</span> <span class="mf">0.</span> <span class="ow">or</span> <span class="n">beta</span> <span class="o">==</span> <span class="mf">1.</span><span class="p">):</span>
            <span class="n">stop</span> <span class="o">=</span> <span class="kc">True</span>
        
        <span class="c1"># Compute the current error based on the squared relative error </span>
        <span class="c1"># suggested by McCain (1990) and update the iteration counter</span>
        <span class="n">err</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">((</span><span class="n">K</span> <span class="o">-</span> <span class="n">K_old</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">/</span> <span class="p">(</span><span class="n">K</span> <span class="o">*</span> <span class="n">K_old</span><span class="p">))</span>
    
    <span class="c1"># Determine the exit condition</span>
    <span class="k">if</span> <span class="n">stop</span><span class="p">:</span>
        <span class="c1"># Successive subsitution thinks this is single-phase</span>
        <span class="n">flag</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
    <span class="k">elif</span> <span class="n">steps</span> <span class="o">&lt;</span> <span class="n">max_iter</span><span class="p">:</span>
        <span class="c1"># This solution is converged</span>
        <span class="n">flag</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># No decision has been reached</span>
        <span class="n">flag</span> <span class="o">=</span> <span class="mi">0</span>
    
    <span class="c1"># Update the equilibrium and return the last value of K-factor</span>
    <span class="n">xi</span><span class="p">,</span> <span class="n">beta</span> <span class="o">=</span> <span class="n">gas_liq_eq</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">K</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">K</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">xi</span><span class="p">,</span> <span class="n">flag</span><span class="p">,</span> <span class="n">steps</span><span class="p">)</span></div>


<div class="viewcode-block" id="gas_liq_eq"><a class="viewcode-back" href="../autodoc/dbm/dbm.gas_liq_eq.html#dbm.gas_liq_eq">[docs]</a><span class="k">def</span> <span class="nf">gas_liq_eq</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">K</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    docstring for gas_liq_eq(m, M, K)</span>
<span class="sd">    </span>
<span class="sd">    This function follows the procedure in Michelsen and Mollerup (2007).  </span>
<span class="sd">    All page and equation numbers below are from this book unless otherwise</span>
<span class="sd">    noted.</span>
<span class="sd">    </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Compute the mole fraction of the total mixture (called the feed in </span>
    <span class="c1"># Michelsen and Mollerup, 2007)</span>
    <span class="n">moles</span> <span class="o">=</span> <span class="n">m</span> <span class="o">/</span> <span class="n">M</span>
    <span class="n">zi</span> <span class="o">=</span> <span class="n">moles</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">moles</span><span class="p">)</span>
    
    <span class="c1"># Define the Rachford-Rice equation for beta as gas fraction.  </span>
    <span class="k">def</span> <span class="nf">g_gas</span><span class="p">(</span><span class="n">beta</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the Rachford-Rice equation, which defines a root-finding </span>
<span class="sd">        problem for the solution of beta, the gas mole fraction in a mixture.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        zi, K = global variables defined in the main function containing this</span>
<span class="sd">            subfunction</span>
<span class="sd">        beta : float</span>
<span class="sd">            Fraction of moles of mixture in the gas phase, [0, 1]</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        g : float</span>
<span class="sd">            Value of the Rachford-Rice equation, the roots of which are the</span>
<span class="sd">            solution for beta.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Equation (2) on page 252</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">zi</span> <span class="o">*</span> <span class="p">(</span><span class="n">K</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">+</span> <span class="n">beta</span> <span class="o">*</span> <span class="p">(</span><span class="n">K</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)))</span>
    
    <span class="k">def</span> <span class="nf">g_gas_p</span><span class="p">(</span><span class="n">beta</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the gradient of the Rachford-Rice equation, which defines a </span>
<span class="sd">        root-finding problem for the solution of beta, the gas mole fraction </span>
<span class="sd">        in a mixture.  This is used in Newton&#39;s method to solve for beta.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        zi, K = global variables defined in the main function containing this</span>
<span class="sd">            subfunction</span>
<span class="sd">        beta : float</span>
<span class="sd">            Fraction of moles of mixture in the gas phase, [0, 1]</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        gp : float</span>
<span class="sd">            Value of the beta-derivative of the Rachford-Rice equation</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Equation (3) on page 252</span>
        <span class="k">return</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">zi</span> <span class="o">*</span> <span class="p">(</span><span class="n">K</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">/</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">+</span> <span class="n">beta</span> <span class="o">*</span> <span class="p">(</span><span class="n">K</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
    
    <span class="c1"># Define the Rachford-Rice equation for beta_l as liquid fraction</span>
    <span class="k">def</span> <span class="nf">g_liq</span><span class="p">(</span><span class="n">beta_l</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the modified Rachford-Rice equation, which defines a root-</span>
<span class="sd">        finding problem for the solution of beta_l, the liquid mole fraction </span>
<span class="sd">        in a mixture.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        zi, K = global variables defined in the main function containing this</span>
<span class="sd">            subfunction</span>
<span class="sd">        beta_l : float</span>
<span class="sd">            Fraction of moles of mixture in the liquid phase, [0, 1]</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        g : float</span>
<span class="sd">            Value of the modified Rachford-Rice equation, the roots of which </span>
<span class="sd">            are the solution for beta_l.</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Unlabeled equation at bottom of page 253</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">zi</span> <span class="o">*</span> <span class="p">(</span><span class="n">K</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">K</span> <span class="o">-</span> <span class="n">beta_l</span> <span class="o">*</span> <span class="p">(</span><span class="n">K</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)))</span>
    
    <span class="k">def</span> <span class="nf">g_liq_p</span><span class="p">(</span><span class="n">beta_l</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the gradient of the modified Rachford-Rice equation, which </span>
<span class="sd">        defines a root-finding problem for the solution of beta_l, the liquid</span>
<span class="sd">        mole fraction in a mixture.  This is used in Newton&#39;s method to solve </span>
<span class="sd">        for beta_l.</span>
<span class="sd">        </span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        zi, K = global variables defined in the main function containing this</span>
<span class="sd">            subfunction</span>
<span class="sd">        beta_l : float</span>
<span class="sd">            Fraction of moles of mixture in the liquid phase, [0, 1]</span>
<span class="sd">        </span>
<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        gp : float</span>
<span class="sd">            Value of the beta_l-derivative of the modified Rachford-Rice </span>
<span class="sd">            equation</span>
<span class="sd">        </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># beta_l-derivative of g_liq equation above</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">zi</span> <span class="o">*</span> <span class="p">(</span><span class="n">K</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">/</span> <span class="p">(</span><span class="n">K</span> <span class="o">-</span> <span class="n">beta_l</span> <span class="o">*</span> <span class="p">(</span><span class="n">K</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>    
    
    <span class="c1"># Step i on page 253:  Check conditions of equations (4) and (5) on page</span>
    <span class="c1"># 252 for existence of a two-phase solution for beta.</span>
    <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">zi</span> <span class="o">*</span> <span class="n">K</span><span class="p">)</span> <span class="o">-</span> <span class="mf">1.</span> <span class="o">&lt;=</span> <span class="mf">0.</span><span class="p">:</span>
        <span class="c1"># This is subcooled liquid, beta = 0.</span>
        <span class="n">beta</span> <span class="o">=</span> <span class="mf">0.</span>
        
    <span class="k">elif</span> <span class="mf">1.</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">zi</span> <span class="o">/</span> <span class="n">K</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mf">0.</span><span class="p">:</span>
        <span class="c1"># This is superheated gas, beta = 1.</span>
        <span class="n">beta</span> <span class="o">=</span> <span class="mf">1.</span>
        
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># This is a two-phase mixture, so search for a solution for beta.</span>
        <span class="c1"># Step ii on page 253:  Check equations (7) and (8) on page 253 for </span>
        <span class="c1"># tighter bounds on the possible range of beta</span>
        <span class="n">beta_min</span> <span class="o">=</span> <span class="mf">0.</span>
        <span class="n">beta_max</span> <span class="o">=</span> <span class="mf">1.</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">K</span><span class="p">)):</span>
            <span class="k">if</span> <span class="n">K</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="mf">1.</span><span class="p">:</span>
                <span class="c1"># Apply equation (7) on page 253</span>
                <span class="n">beta_min</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">([</span><span class="n">beta_min</span><span class="p">,</span> <span class="p">(</span><span class="n">K</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">zi</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span> <span class="o">/</span> 
                           <span class="p">(</span><span class="n">K</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Apply equation (8) on page 253</span>
                <span class="n">beta_max</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">([</span><span class="n">beta_max</span><span class="p">,</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">-</span> <span class="n">zi</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">/</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">-</span> <span class="n">K</span><span class="p">[</span><span class="n">i</span><span class="p">])])</span>
        
        <span class="c1"># Step iii on page 254:  Select initial guess for beta and choose</span>
        <span class="c1"># which objective function to use.</span>
        <span class="n">beta</span> <span class="o">=</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="n">beta_min</span> <span class="o">+</span> <span class="n">beta_max</span><span class="p">)</span>
        
        <span class="k">if</span> <span class="n">g_gas</span><span class="p">(</span><span class="n">beta</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mf">0.</span><span class="p">:</span>
            <span class="c1"># Solution will have excess gas</span>
            <span class="n">eqn</span> <span class="o">=</span> <span class="mf">1.</span>
            <span class="n">beta_var</span> <span class="o">=</span> <span class="n">beta</span>
            
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Solution will have excess liquid</span>
            <span class="n">eqn</span> <span class="o">=</span> <span class="mf">0.</span>
            <span class="n">beta_var</span> <span class="o">=</span> <span class="mf">1.</span> <span class="o">-</span> <span class="n">beta</span>
            <span class="n">beta_min_hold</span> <span class="o">=</span> <span class="n">beta_min</span>
            <span class="n">beta_min</span> <span class="o">=</span> <span class="mf">1.</span> <span class="o">-</span> <span class="n">beta_max</span>
            <span class="n">beta_max</span> <span class="o">=</span> <span class="mf">1.</span> <span class="o">-</span> <span class="n">beta_min_hold</span>
        
        <span class="c1"># Set up an iterative solution to find beta_var using the optimal</span>
        <span class="c1"># root-finding equation</span>
        <span class="n">tol</span> <span class="o">=</span> <span class="mf">1.e-8</span>
        <span class="n">err</span> <span class="o">=</span> <span class="mf">1.</span>
        
        <span class="k">while</span> <span class="n">err</span> <span class="o">&gt;</span> <span class="n">tol</span><span class="p">:</span>
            <span class="c1"># Store the current value of beta</span>
            <span class="n">beta_old</span> <span class="o">=</span> <span class="n">beta_var</span>
            
            <span class="c1"># Step iv on page 254:  Perform one iteration of Newton&#39;s method</span>
            <span class="c1"># and narrow the possible range of the solution for beta</span>
            <span class="k">if</span> <span class="n">eqn</span> <span class="o">&gt;</span> <span class="mf">0.</span><span class="p">:</span>
                <span class="c1"># Use the equations for excess gas</span>
                <span class="n">g</span> <span class="o">=</span> <span class="n">g_gas</span><span class="p">(</span><span class="n">beta_var</span><span class="p">)</span>
                <span class="n">gp</span> <span class="o">=</span> <span class="n">g_gas_p</span><span class="p">(</span><span class="n">beta_var</span><span class="p">)</span>
                <span class="n">beta_new</span> <span class="o">=</span> <span class="n">beta_var</span> <span class="o">-</span> <span class="n">g</span> <span class="o">/</span> <span class="n">gp</span>
                
                <span class="c1"># Update bounds on beta per criteria in step iv on page 254</span>
                <span class="k">if</span> <span class="n">g</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">beta_min</span> <span class="o">=</span> <span class="n">beta_var</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">beta_max</span> <span class="o">=</span> <span class="n">beta_var</span>
            
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Use the equations for excess liqiud</span>
                <span class="n">g</span> <span class="o">=</span> <span class="n">g_liq</span><span class="p">(</span><span class="n">beta_var</span><span class="p">)</span>
                <span class="n">gp</span> <span class="o">=</span> <span class="n">g_liq_p</span><span class="p">(</span><span class="n">beta_var</span><span class="p">)</span>
                <span class="n">beta_new</span> <span class="o">=</span> <span class="n">beta_var</span> <span class="o">-</span> <span class="n">g</span> <span class="o">/</span> <span class="n">gp</span>
                
                <span class="c1"># Update bounds on beta per criteria in step iv on page 254</span>
                <span class="k">if</span> <span class="n">g</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">beta_max</span> <span class="o">=</span> <span class="n">beta_var</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">beta_min</span> <span class="o">=</span> <span class="n">beta_var</span>
            
            <span class="c1"># Step v on page 254:  Select best update for beta</span>
            <span class="k">if</span> <span class="n">beta_new</span> <span class="o">&lt;=</span> <span class="n">beta_max</span> <span class="ow">and</span> <span class="n">beta_new</span> <span class="o">&gt;=</span> <span class="n">beta_min</span><span class="p">:</span>
                <span class="c1"># Newton&#39;s method is converging within allowable range for </span>
                <span class="c1"># the independent variable:  use the Newton&#39;s method solution</span>
                <span class="n">beta_var</span> <span class="o">=</span> <span class="n">beta_new</span>
                
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Newton&#39;s method suggests a solution outside the allowable</span>
                <span class="c1"># range for the independent variable:  use the bisection</span>
                <span class="c1"># method</span>
                <span class="n">beta_var</span> <span class="o">=</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="n">beta_min</span> <span class="o">+</span> <span class="n">beta_max</span><span class="p">)</span>
            
            <span class="c1"># Step vi on page 254:  Check for convergence.  Note:  do not</span>
            <span class="c1"># use relative error since beta_var ~= 0 is an acceptable answer</span>
            <span class="n">err</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">beta_var</span> <span class="o">-</span> <span class="n">beta_old</span><span class="p">)</span>
        
        <span class="c1"># Get the final value of beta, the gas mole fraction</span>
        <span class="k">if</span> <span class="n">eqn</span> <span class="o">&gt;</span> <span class="mf">0.</span><span class="p">:</span>
            <span class="c1"># We found beta</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="n">beta_var</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># We found beta_l</span>
            <span class="n">beta</span> <span class="o">=</span> <span class="mf">1.</span> <span class="o">-</span> <span class="n">beta_var</span>
    
    <span class="c1"># Return the solution for gas and liquid mole fractions based on the</span>
    <span class="c1"># converged value of beta</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">zi</span> <span class="o">*</span> <span class="n">K</span> <span class="o">/</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">+</span> <span class="n">beta</span> <span class="o">*</span> <span class="p">(</span><span class="n">K</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)),</span> 
                     <span class="n">zi</span> <span class="o">/</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">+</span> <span class="n">beta</span> <span class="o">*</span> <span class="p">(</span><span class="n">K</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">))]),</span> <span class="n">beta</span><span class="p">)</span></div>

</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">Texas A&M Oil spill / Outfall Calculator</a></h1>








<h3>Navigation</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../user_manual.html">TAMOC User Manual</a></li>
<li class="toctree-l1"><a class="reference internal" href="../unit_tests.html">Unit Tests</a></li>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../bugs.html">Reporting Bugs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../readme.html">Read Me File</a></li>
<li class="toctree-l1"><a class="reference internal" href="../release.html">Release Notes</a></li>
<li class="toctree-l1"><a class="reference internal" href="../license.html">License</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../index.html">Documentation overview</a><ul>
  <li><a href="index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2020, Scott A. Socolofsky.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 2.4.4</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
      
    </div>

    

    
  </body>
</html>